From 127102a3888c8031b5a6b2d5b98b3193d6d11926 Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Thu, 18 Jun 2026 13:18:46 +0330 Subject: [PATCH] chatClient completed. --- .../chat/Client/ServerListener.java | 6 +- .../university/chat/Client/chatClient.java | 117 ++++++++++++++---- .../university/chat/Server/ClientSession.java | 1 - 3 files changed, 100 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/university/chat/Client/ServerListener.java b/src/main/java/com/university/chat/Client/ServerListener.java index b609d74..87eab36 100644 --- a/src/main/java/com/university/chat/Client/ServerListener.java +++ b/src/main/java/com/university/chat/Client/ServerListener.java @@ -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) @@ -17,4 +21,4 @@ public class ServerListener implements Runnable{ System.out.println("Disconnected from server"); } } -} +} \ No newline at end of file diff --git a/src/main/java/com/university/chat/Client/chatClient.java b/src/main/java/com/university/chat/Client/chatClient.java index dae0bad..96e92fc 100644 --- a/src/main/java/com/university/chat/Client/chatClient.java +++ b/src/main/java/com/university/chat/Client/chatClient.java @@ -1,29 +1,102 @@ package com.university.chat.Client; -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. +import com.university.chat.Common.ChatMessage; +import com.university.chat.Common.FileMessage; +import com.university.chat.Common.MessageType; - while (true){ - try { - // TODO: Program loop — read a line from the console and act on it: - // - "/msg " -> build & send a PRIVATE_MESSAGE - // - "/users" -> build & send a USER_LIST request - // - "/sendfile " -> 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(). - } catch (Exception e){ - System.out.println("command failed: " + e.getMessage()); +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() 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 { + 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 "); + 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 "); + 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(); } } } diff --git a/src/main/java/com/university/chat/Server/ClientSession.java b/src/main/java/com/university/chat/Server/ClientSession.java index 0daa8ee..b3ebdda 100644 --- a/src/main/java/com/university/chat/Server/ClientSession.java +++ b/src/main/java/com/university/chat/Server/ClientSession.java @@ -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);