From f407f841ebe77adcae1dd2e6df82234320bf86f0 Mon Sep 17 00:00:00 2001 From: Fateme Azizi Date: Mon, 22 Jun 2026 02:42:34 +0330 Subject: [PATCH] implement chatClient.java --- .../university/chat/Client/chatClient.java | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/university/chat/Client/chatClient.java b/src/main/java/com/university/chat/Client/chatClient.java index dae0bad..fcb3c31 100644 --- a/src/main/java/com/university/chat/Client/chatClient.java +++ b/src/main/java/com/university/chat/Client/chatClient.java @@ -1,7 +1,18 @@ 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.*; +import java.net.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; + public class chatClient { - public static void main() { + public static void main() throws IOException { // TODO: Connecting to the server // 1. Create a socket and connect to the server // 2. Create an ObjectOutputStream (out) and ObjectInputStream (in) @@ -10,6 +21,22 @@ public class chatClient { // 3. Start a new Thread running a ServerListener(in) so incoming // messages are handled concurrently. + Socket socket = new Socket("127.0.0.1", 5000); + ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); + ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); + + Scanner input = new Scanner(System.in); + System.out.println("Enter your username: "); + String username = input.nextLine(); + + ChatMessage chatMessage = new ChatMessage(MessageType.LOGIN, username, null, ""); + out.writeObject(chatMessage); + out.flush(); + + Thread thread = new Thread(new ServerListener(in)); + thread.start(); + + while (true){ try { // TODO: Program loop — read a line from the console and act on it: @@ -21,6 +48,38 @@ public class chatClient { // and send it as a FileMessage // - anything else -> send a PUBLIC_MESSAGE // Remember to flush() the output stream after writeObject(). + + String line = input.nextLine(); + if(line.equals("/users")) { + chatMessage = new ChatMessage(MessageType.USER_LIST, username, null, null); + out.writeObject(chatMessage); + out.flush(); + } + else if(line.startsWith("/msg")) { + String[] parts = line.split(" ", 3); + chatMessage = new ChatMessage(MessageType.PRIVATE_MESSAGE, + username, parts[1], parts[2]); + out.writeObject(chatMessage); + out.flush(); + } + else if(line.startsWith("/sendfile")) { + String[] parts = line.split(" ", 3); + String receiver = parts[1]; + String path = parts[2]; + + Path filePath = Paths.get(path); + byte[] data = Files.readAllBytes(filePath); + FileMessage fileMessage = new FileMessage(username, receiver, filePath.getFileName().toString(), data); + + out.writeObject(fileMessage); + out.flush(); + } + else { + chatMessage = new ChatMessage(MessageType.PUBLIC_MESSAGE, username, null, line); + out.writeObject(chatMessage); + out.flush(); + } + } catch (Exception e){ System.out.println("command failed: " + e.getMessage()); }