implement chatClient.java

This commit is contained in:
2026-06-22 02:42:34 +03:30
parent 9a5bddd862
commit f407f841eb
@@ -1,7 +1,18 @@
package com.university.chat.Client; 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 class chatClient {
public static void main() { public static void main() throws IOException {
// TODO: Connecting to the server // TODO: Connecting to the server
// 1. Create a socket and connect to the server // 1. Create a socket and connect to the server
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in) // 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 // 3. Start a new Thread running a ServerListener(in) so incoming
// messages are handled concurrently. // 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){ while (true){
try { try {
// TODO: Program loop — read a line from the console and act on it: // 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 // and send it as a FileMessage
// - anything else -> send a PUBLIC_MESSAGE // - anything else -> send a PUBLIC_MESSAGE
// Remember to flush() the output stream after writeObject(). // 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){ } catch (Exception e){
System.out.println("command failed: " + e.getMessage()); System.out.println("command failed: " + e.getMessage());
} }