This commit is contained in:
2026-06-20 01:17:57 +04:30
parent 240eac0504
commit cfce2a952e
4 changed files with 214 additions and 20 deletions
@@ -1,7 +1,19 @@
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.awt.*;
import java.io.*;
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() {
public static void main() throws IOException {
// TODO: Connecting to the server
// 1. Create a socket and connect to the server
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in)
@@ -10,20 +22,89 @@ public class chatClient {
// 3. Start a new Thread running a ServerListener(in) so incoming
// messages are handled concurrently.
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());
String username;
try (Socket socket = new Socket("localhost", 12345)) { //______________
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Scanner keyboard = new Scanner(System.in);
username = keyboard.nextLine();
out.writeObject(new ChatMessage(MessageType.LOGIN, username, "System", "Login"));
out.flush();
ServerListener serverListener = new ServerListener(in);
Thread ListenerThread = new Thread(serverListener);
ListenerThread.setDaemon(true);
ListenerThread.start();
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().
String consoleLine;
if ((consoleLine = keyboard.nextLine()) != null){
if (consoleLine.startsWith("/msg")) {
int i1 = consoleLine.indexOf("<");
int i2 = consoleLine.indexOf(">");
int j1 = consoleLine.lastIndexOf("<");
int j2 = consoleLine.lastIndexOf(">");
String user = consoleLine.substring(i1+1, i2);
String text = consoleLine.substring(j1+1, j2);
ChatMessage chatMessage = new ChatMessage(MessageType.PRIVATE_MESSAGE, username, user, text);
out.writeObject(chatMessage);
out.flush();
} else if (consoleLine.startsWith("/users")) {
ChatMessage chatMessage = new ChatMessage(MessageType.USER_LIST, username, username, "users_list");
out.writeObject(chatMessage);
out.flush();
} else if (consoleLine.startsWith("/sendfile")) {
int i1 = consoleLine.indexOf("<");
int i2 = consoleLine.indexOf(">");
int j1 = consoleLine.lastIndexOf("<");
int j2 = consoleLine.lastIndexOf(">");
String user = consoleLine.substring(i1+1, i2);
String pathStr = consoleLine.substring(j1+1, j2);
Path path = Paths.get(pathStr);
byte[] data = Files.readAllBytes(path);
FileMessage fileMessage = new FileMessage(username, user, "filename", data);
out.writeObject(fileMessage);
out.flush();
} else {
ChatMessage chatMessage = new ChatMessage(MessageType.PUBLIC_MESSAGE, username, "everyone", consoleLine);
out.writeObject(chatMessage);
out.flush();
}
}
} catch (Exception e) {
System.out.println("command failed: " + e.getMessage());
}
}
}
catch (Exception e) {
//______________
}
}
}