forked from AdvancedProgramming1404/HW-10-Socket-Programming
chatClient completed.
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
package com.university.chat.Client;
|
package com.university.chat.Client;
|
||||||
|
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
|
||||||
public class ServerListener implements Runnable{
|
public class ServerListener implements Runnable{
|
||||||
|
public ServerListener(ObjectInputStream in) {
|
||||||
|
}
|
||||||
// TODO: store the ObjectInputStream from the user socket
|
// TODO: store the ObjectInputStream from the user socket
|
||||||
// (this should be the same input stream the
|
// (this should be the same input stream the
|
||||||
// chatClient created when connecting)
|
// chatClient created when connecting)
|
||||||
@@ -17,4 +21,4 @@ public class ServerListener implements Runnable{
|
|||||||
System.out.println("Disconnected from server");
|
System.out.println("Disconnected from server");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,29 +1,102 @@
|
|||||||
package com.university.chat.Client;
|
package com.university.chat.Client;
|
||||||
|
|
||||||
public class chatClient {
|
import com.university.chat.Common.ChatMessage;
|
||||||
public static void main() {
|
import com.university.chat.Common.FileMessage;
|
||||||
// TODO: Connecting to the server
|
import com.university.chat.Common.MessageType;
|
||||||
// 1. Create a socket and connect to the server
|
|
||||||
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in)
|
|
||||||
// from the socket's streams — output FIRST, then input.
|
|
||||||
// 2. Get the username, and send a LOGIN ChatMessage with that username
|
|
||||||
// 3. Start a new Thread running a ServerListener(in) so incoming
|
|
||||||
// messages are handled concurrently.
|
|
||||||
|
|
||||||
while (true){
|
import java.io.File;
|
||||||
try {
|
import java.io.IOException;
|
||||||
// TODO: Program loop — read a line from the console and act on it:
|
import java.io.ObjectInputStream;
|
||||||
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
|
import java.io.ObjectOutputStream;
|
||||||
// - "/users" -> build & send a USER_LIST request
|
import java.net.Socket;
|
||||||
// - "/sendfile <user> <path>" -> read the file into a byte[]
|
import java.nio.file.Files;
|
||||||
// (you can use TransferProgress
|
import java.nio.file.Path;
|
||||||
// to show progress)
|
import java.util.Scanner;
|
||||||
// and send it as a FileMessage
|
|
||||||
// - anything else -> send a PUBLIC_MESSAGE
|
public class chatClient {
|
||||||
// Remember to flush() the output stream after writeObject().
|
public static void main() throws IOException {
|
||||||
} catch (Exception e){
|
|
||||||
System.out.println("command failed: " + e.getMessage());
|
String host = "localhost";
|
||||||
|
int port = 8080;
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("Please enter your name: ");
|
||||||
|
String username = scanner.nextLine();
|
||||||
|
|
||||||
|
try (Socket socket = new Socket(host, port);) {
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||||
|
|
||||||
|
ChatMessage loginMessage = new ChatMessage(MessageType.LOGIN,"Server", username, "Login successful.");
|
||||||
|
out.writeObject(loginMessage);
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
ServerListener serverListener = new ServerListener(in);
|
||||||
|
Thread listenerThread = new Thread(serverListener);
|
||||||
|
listenerThread.start();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
String input = scanner.nextLine().trim();
|
||||||
|
|
||||||
|
if (input.isEmpty()) {continue;}
|
||||||
|
|
||||||
|
if (input.startsWith("/msg ")) {
|
||||||
|
String[] parts = input.split(" ");
|
||||||
|
|
||||||
|
if (parts.length < 3) {
|
||||||
|
System.out.println("Usage: /msg <user> <text>");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String target = parts[1];
|
||||||
|
String msg = parts[2];
|
||||||
|
|
||||||
|
ChatMessage privateMessage = new ChatMessage(MessageType.PRIVATE_MESSAGE, username, target, msg);
|
||||||
|
out.writeObject(privateMessage);
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
} else if (input.equalsIgnoreCase("/users")) {
|
||||||
|
ChatMessage usersList = new ChatMessage(MessageType.USER_LIST, username, "Server", "Users list request.");
|
||||||
|
out.writeObject(usersList);
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
} else if (input.startsWith("/sendfile ")) {
|
||||||
|
String[] parts = input.split(" ");
|
||||||
|
|
||||||
|
if (parts.length < 3) {
|
||||||
|
System.out.println("Usage: /sendfile <user> <text>");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String target = parts[1];
|
||||||
|
String filePathStr = parts[2];
|
||||||
|
|
||||||
|
File file = new File(filePathStr);
|
||||||
|
if (!file.exists() || !file.isFile()) {
|
||||||
|
System.out.println("File not found: " + filePathStr);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] fileData = Files.readAllBytes(Path.of(filePathStr));
|
||||||
|
|
||||||
|
FileMessage fileMessage = new FileMessage(username, target, filePathStr, fileData);
|
||||||
|
out.writeObject(fileMessage);
|
||||||
|
out.flush();
|
||||||
|
System.out.println("File sent successfully.");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ChatMessage publicMsg = new ChatMessage(MessageType.PUBLIC_MESSAGE, username, null, input);
|
||||||
|
out.writeObject(publicMsg);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e){
|
||||||
|
System.out.println("command failed: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -169,7 +169,6 @@ public class ClientSession implements Runnable {
|
|||||||
Files.write(sentPath, fileMsg.getData());
|
Files.write(sentPath, fileMsg.getData());
|
||||||
Files.write(recvPath, fileMsg.getData());
|
Files.write(recvPath, fileMsg.getData());
|
||||||
|
|
||||||
// TODO: Forward the received file-message to the destination user.
|
|
||||||
String receiverName = fileMsg.getReceiver();
|
String receiverName = fileMsg.getReceiver();
|
||||||
ClientSession receiverSession = ChatServer.userManager.getUser(receiverName);
|
ClientSession receiverSession = ChatServer.userManager.getUser(receiverName);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user