From 9e0047ccd995a68a7965923aae987b9b48f125ae Mon Sep 17 00:00:00 2001 From: HadiSharifi Date: Sun, 14 Jun 2026 10:26:49 +0330 Subject: [PATCH] implement chatclient class --- .../chat/Client/ServerListener.java | 2 +- .../university/chat/Client/chatClient.java | 108 ++++++++++++++---- 2 files changed, 89 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/university/chat/Client/ServerListener.java b/src/main/java/com/university/chat/Client/ServerListener.java index ff85047..9e6914d 100644 --- a/src/main/java/com/university/chat/Client/ServerListener.java +++ b/src/main/java/com/university/chat/Client/ServerListener.java @@ -17,7 +17,7 @@ public class ServerListener implements Runnable{ @Override public void run() { try { - + while (true) { Object obj = in.readObject(); if (obj instanceof ChatMessage msg) { diff --git a/src/main/java/com/university/chat/Client/chatClient.java b/src/main/java/com/university/chat/Client/chatClient.java index dae0bad..226842f 100644 --- a/src/main/java/com/university/chat/Client/chatClient.java +++ b/src/main/java/com/university/chat/Client/chatClient.java @@ -1,29 +1,97 @@ 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.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.Socket; +import java.nio.file.Files; +import java.nio.file.Paths; +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. - 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()); + String host = "localhost"; + int port = 9000; + + try { + + Socket socket = new Socket(host, port); + ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); + ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); + + Scanner sc = new Scanner(System.in); + System.out.println("Enter your username: "); + String username = sc.nextLine(); + + out.writeObject(new ChatMessage(MessageType.LOGIN, username, null, null)); + out.flush(); + + Object loginResponse = in.readObject(); + if (loginResponse instanceof ChatMessage response) { + if (response.getType() == MessageType.LOGIN_FAILED) { + System.out.println("Login failed: " + response.getContent()); + socket.close(); + return; + } + System.out.println(response.getContent()); } + + Thread listenerThread = new Thread(new ServerListener(in)); + listenerThread.setDaemon(true); + listenerThread.start(); + + while (true) { + try { + String line = sc.nextLine(); + + if (line.startsWith("/msg ")) { + String[] parts = line.split(" ", 3); + if (parts.length < 3) { + System.out.println("Usage: /msg "); + continue; + } + String target = parts[1]; + String text = parts[2]; + out.writeObject(new ChatMessage(MessageType.PRIVATE_MESSAGE, username, target, text)); + out.flush(); + + } else if (line.equals("/users")) { + out.writeObject(new ChatMessage(MessageType.USER_LIST, username, null, null)); + out.flush(); + + } else if (line.startsWith("/sendfile ")) { + String[] parts = line.split(" ", 3); + if (parts.length < 3) { + System.out.println("Usage: /sendfile "); + continue; + } + String target = parts[1]; + String filePath = parts[2]; + + byte[] fileData = Files.readAllBytes(Paths.get(filePath)); + String filename = Paths.get(filePath).getFileName().toString(); + + TransferProgress progress = new TransferProgress(fileData.length); + progress.update(fileData.length); + + out.writeObject(new FileMessage(username, target, filename, fileData)); + out.flush(); + System.out.println("File '" + filename + "' sent to " + target + "."); + + } else { + out.writeObject(new ChatMessage(MessageType.PUBLIC_MESSAGE, username, null, line)); + out.flush(); + } + } catch (Exception e) { + System.out.println("command failed: " + e.getMessage()); + } + } + } catch (Exception e) { + System.out.println(" could not connect to " + host + ":" + port); } } }