This commit is contained in:
Reza
2026-06-19 13:44:24 +03:30
parent 240eac0504
commit d6b7215667
4 changed files with 192 additions and 66 deletions
@@ -1,18 +1,32 @@
package com.university.chat.Client;
public class ServerListener implements Runnable{
// TODO: store the ObjectInputStream from the user socket
// (this should be the same input stream the
// chatClient created when connecting)
import com.university.chat.Common.ChatMessage;
import com.university.chat.Common.FileMessage;
import java.io.ObjectInputStream;
public class ServerListener implements Runnable{
private final ObjectInputStream in;
public ServerListener(ObjectInputStream in) {
this.in = in;
}
@Override
public void run() {
try {
// TODO: In an infinite loop read objects from the server
// - if it's a ChatMessage -> print "<sender>: <content>"
// - if it's a FileMessage -> print that a file was received
// (filename + sender), it's already
// saved to disk by the server.
while (true) {
Object obj = in.readObject();
if (obj instanceof ChatMessage msg) {
if (msg.getType() == com.university.chat.Common.MessageType.USER_LIST) {
System.out.println("Online users: " + msg.getContent());
} else {
System.out.println(msg.getSender() + ": " + msg.getContent());
}
} else if (obj instanceof FileMessage fileMsg) {
System.out.println("File received: " + fileMsg.getFilename()
+ " from " + fileMsg.getSender());
}
}
} catch (Exception e){
System.out.println("Disconnected from server");
}
@@ -1,29 +1,81 @@
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;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
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());
public class chatClient {
public static void main(String[] args) {
String serverAddress = "localhost";
int serverPort = 12345;
try (Socket socket = new Socket(serverAddress, serverPort);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Scanner console = new Scanner(System.in)) {
System.out.print("Enter username: ");
String username = console.nextLine();
out.writeObject(new ChatMessage(MessageType.LOGIN, username, null, null));
out.flush();
ChatMessage response = (ChatMessage) in.readObject();
if (response.getType() == MessageType.LOGIN_FAILED) {
System.out.println("Login failed: " + response.getContent());
return;
}
System.out.println("Connected as " + username);
Thread listener = new Thread(new ServerListener(in));
listener.start();
while (true) {
try {
String line = console.nextLine();
if (line.startsWith("/msg ")) {
String[] parts = line.split(" ", 3);
if (parts.length < 3) {
System.out.println("Usage: /msg <user> <message>");
continue;
}
out.writeObject(new ChatMessage(MessageType.PRIVATE_MESSAGE, username, parts[1], parts[2]));
}
else if (line.equals("/users")) {
out.writeObject(new ChatMessage(MessageType.USER_LIST, username, null, null));
}
else if (line.startsWith("/sendfile ")) {
String[] parts = line.split(" ", 3);
if (parts.length < 3) {
System.out.println("Usage: /sendfile <user> <filepath>");
continue;
}
try {
byte[] data = Files.readAllBytes(Path.of(parts[2]));
out.writeObject(new FileMessage(username, parts[1],
Path.of(parts[2]).getFileName().toString(), data));
}
catch (IOException e) {
System.out.println("File error: " + e.getMessage());
}
}
else {
out.writeObject(new ChatMessage(MessageType.PUBLIC_MESSAGE, username, null, line));
}
out.flush();
}
catch (Exception e) {
System.out.println("command failed: " + e.getMessage());
}
}
} catch (Exception e) {
System.out.println("Connection closed: " + e.getMessage());
}
}
}
}