From 2d9883e6844bdc927a6fa820996ed3029ca5d970 Mon Sep 17 00:00:00 2001 From: Parmis Jamami Date: Tue, 23 Jun 2026 23:41:25 +0330 Subject: [PATCH] implement ChatClient --- .../university/chat/Client/chatClient.java | 123 ++++++++++++++---- 1 file changed, 100 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..955e4c7 100644 --- a/src/main/java/com/university/chat/Client/chatClient.java +++ b/src/main/java/com/university/chat/Client/chatClient.java @@ -1,29 +1,106 @@ 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.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.Socket; +import java.nio.file.Files; +import java.util.Scanner; + +public class chatClient { + + public static void main(String[] args) { + try { + Socket socket = new Socket("localhost", 5000); + + ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); + ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); + + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter username: "); + String username = scanner.nextLine(); + + ChatMessage loginMessage = new ChatMessage( + MessageType.LOGIN, + username, + null, + "" + ); + + out.writeObject(loginMessage); + out.flush(); + + new Thread(new ServerListener(in)).start(); + + while (true) { + try { + String line = scanner.nextLine(); + + if (line.startsWith("/msg ")) { + String[] parts = line.split(" ", 3); + + ChatMessage privateMessage = new ChatMessage( + MessageType.PRIVATE_MESSAGE, + username, + parts[1], + parts[2] + ); + + out.writeObject(privateMessage); + } + + else if (line.equals("/users")) { + ChatMessage userListMessage = new ChatMessage( + MessageType.USER_LIST, + username, + null, + "" + ); + + out.writeObject(userListMessage); + } + + else if (line.startsWith("/sendfile ")) { + String[] parts = line.split(" ", 3); + + File file = new File(parts[2]); + byte[] data = Files.readAllBytes(file.toPath()); + + FileMessage fileMessage = new FileMessage( + username, + parts[1], + file.getName(), + data + ); + + out.writeObject(fileMessage); + } + + else { + ChatMessage publicMessage = new ChatMessage( + MessageType.PUBLIC_MESSAGE, + username, + null, + line + ); + + out.writeObject(publicMessage); + } + + out.flush(); + + } catch (Exception e) { + System.out.println("command failed: " + e.getMessage()); + } } + + } catch (Exception e) { + System.out.println("Could not connect to server"); } } -} +} \ No newline at end of file