From 87a5ca499d7fff1e4b3f0eac99135ebbe4f53b14 Mon Sep 17 00:00:00 2001 From: ShayanEdalatjoo Date: Fri, 17 Jul 2026 18:27:41 +0330 Subject: [PATCH] implementing chatClient.java --- .../university/chat/Client/chatClient.java | 125 ++++++++++++++---- 1 file changed, 102 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/university/chat/Client/chatClient.java b/src/main/java/com/university/chat/Client/chatClient.java index dae0bad..ae8b833 100644 --- a/src/main/java/com/university/chat/Client/chatClient.java +++ b/src/main/java/com/university/chat/Client/chatClient.java @@ -1,29 +1,108 @@ 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.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.nio.file.Paths; +import java.util.Scanner; + +public class chatClient { + + public static void main() { + + final String SERVER_HOST = "localhost"; + final int SERVER_PORT = 600; + + Scanner input = new Scanner(System.in); + + try { + Socket clientSocket = new Socket(SERVER_HOST, SERVER_PORT); + + ObjectOutputStream output = new ObjectOutputStream(clientSocket.getOutputStream()); + output.flush(); + ObjectInputStream inputStream = new ObjectInputStream(clientSocket.getInputStream()); + + System.out.print("Username: "); + String username = input.nextLine(); + + output.writeObject(new ChatMessage(MessageType.LOGIN, username, null, null)); + output.flush(); + + Thread serverThread = new Thread(new ServerListener(inputStream)); + serverThread.start(); + + while (true) { + try { + String command = input.nextLine(); + + if (command.startsWith("/msg ")) { + String[] args = command.substring(5).split(" ", 2); + + String recipient = args[0]; + String message = args.length > 1 ? args[1] : ""; + + output.writeObject(new ChatMessage( + MessageType.PRIVATE_MESSAGE, + username, + recipient, + message)); + + output.flush(); + continue; + } + + if (command.equals("/users")) { + output.writeObject(new ChatMessage( + MessageType.USER_LIST, + username, + null, + null)); + + output.flush(); + continue; + } + + if (command.startsWith("/sendfile ")) { + String[] args = command.substring(10).split(" ", 2); + + String recipient = args[0]; + Path file = Paths.get(args[1]); + + byte[] fileData = Files.readAllBytes(file); + String fileName = file.getFileName().toString(); + + output.writeObject(new FileMessage( + username, + recipient, + fileName, + fileData)); + + output.flush(); + continue; + } + + output.writeObject(new ChatMessage( + MessageType.PUBLIC_MESSAGE, + username, + null, + command)); + + output.flush(); + + } catch (Exception ex) { + System.out.println("Command failed: " + ex.getMessage()); + } } + + } catch (IOException ex) { + System.out.println(ex.getMessage()); } } -} +} \ No newline at end of file