ServerListener completed.

This commit is contained in:
2026-06-18 13:51:50 +03:30
parent 127102a388
commit ab36719547
2 changed files with 24 additions and 10 deletions
@@ -1,24 +1,38 @@
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.Server.ClientSession;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
public class ServerListener implements Runnable{ public class ServerListener implements Runnable{
private final ObjectInputStream in;
public ServerListener(ObjectInputStream in) { public ServerListener(ObjectInputStream in) {
this.in = in;
} }
// TODO: store the ObjectInputStream from the user socket
// (this should be the same input stream the
// chatClient created when connecting)
@Override @Override
public void run() { public void run() {
try { try {
// TODO: In an infinite loop read objects from the server while (true) {
// - if it's a ChatMessage -> print "<sender>: <content>" Object incomingObject = in.readObject();
// - if it's a FileMessage -> print that a file was received
// (filename + sender), it's already if (incomingObject instanceof ChatMessage) {
// saved to disk by the server. ChatMessage message = (ChatMessage) incomingObject;
System.out.println(message.getSender() + ": " + message.getContent());
} else if (incomingObject instanceof FileMessage) {
FileMessage fileMessage = (FileMessage) incomingObject;
System.out.println("[FILE RECEIVE] You received a file: '"
+ fileMessage.getFilename() + "' from " + fileMessage.getSender()
+ " (Saved on server disk).");
}
}
} catch (Exception e){ } catch (Exception e){
System.out.println("Disconnected from server"); System.out.println("Disconnected from server.");
} }
} }
} }
@@ -15,7 +15,7 @@ import java.util.Scanner;
public class chatClient { public class chatClient {
public static void main() throws IOException { public static void main() throws IOException {
String host = "localhost"; String host = "localhost";
int port = 8080; int port = 8080;
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);