implement ChatClient

This commit is contained in:
2026-06-23 23:41:25 +03:30
parent 5a759ae02f
commit 2d9883e684
@@ -1,29 +1,106 @@
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.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 class chatClient {
public static void main() {
// TODO: Connecting to the server public static void main(String[] args) {
// 1. Create a socket and connect to the server try {
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in) Socket socket = new Socket("localhost", 5000);
// from the socket's streams — output FIRST, then input.
// 2. Get the username, and send a LOGIN ChatMessage with that username ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
// 3. Start a new Thread running a ServerListener(in) so incoming ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
// messages are handled concurrently.
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) { while (true) {
try { try {
// TODO: Program loop — read a line from the console and act on it: String line = scanner.nextLine();
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
// - "/users" -> build & send a USER_LIST request if (line.startsWith("/msg ")) {
// - "/sendfile <user> <path>" -> read the file into a byte[] String[] parts = line.split(" ", 3);
// (you can use TransferProgress
// to show progress) ChatMessage privateMessage = new ChatMessage(
// and send it as a FileMessage MessageType.PRIVATE_MESSAGE,
// - anything else -> send a PUBLIC_MESSAGE username,
// Remember to flush() the output stream after writeObject(). 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) { } catch (Exception e) {
System.out.println("command failed: " + e.getMessage()); System.out.println("command failed: " + e.getMessage());
} }
} }
} catch (Exception e) {
System.out.println("Could not connect to server");
}
} }
} }