forked from AdvancedProgramming1404/HW-10-Socket-Programming
chatClient completed.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package com.university.chat.Client;
|
||||
|
||||
import java.io.ObjectInputStream;
|
||||
|
||||
public class ServerListener implements Runnable{
|
||||
public ServerListener(ObjectInputStream in) {
|
||||
}
|
||||
// TODO: store the ObjectInputStream from the user socket
|
||||
// (this should be the same input stream the
|
||||
// chatClient created when connecting)
|
||||
|
||||
@@ -1,29 +1,102 @@
|
||||
package com.university.chat.Client;
|
||||
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Common.FileMessage;
|
||||
import com.university.chat.Common.MessageType;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class chatClient {
|
||||
public static void main() {
|
||||
// TODO: Connecting to the server
|
||||
// 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.
|
||||
public static void main() throws IOException {
|
||||
|
||||
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 {
|
||||
// TODO: Program loop — read a line from the console and act on it:
|
||||
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
|
||||
// - "/users" -> build & send a USER_LIST request
|
||||
// - "/sendfile <user> <path>" -> read the file into a byte[]
|
||||
// (you can use TransferProgress
|
||||
// to show progress)
|
||||
// and send it as a FileMessage
|
||||
// - anything else -> send a PUBLIC_MESSAGE
|
||||
// Remember to flush() the output stream after writeObject().
|
||||
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(recvPath, fileMsg.getData());
|
||||
|
||||
// TODO: Forward the received file-message to the destination user.
|
||||
String receiverName = fileMsg.getReceiver();
|
||||
ClientSession receiverSession = ChatServer.userManager.getUser(receiverName);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user