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;
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 <user> <text>" -> build & send a PRIVATE_MESSAGE
// - "/users" -> build & send a USER_LIST request
// - "/sendfile <user> <path>" -> 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");
}
}
}
}