feat: complete TODO tasks in ChatServer.java

This commit is contained in:
2026-06-24 13:07:14 +03:30
parent bda8e144ff
commit 47b2c3f85f
@@ -1,15 +1,37 @@
package com.university.chat.Server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer {
// TODO: declare a single shared UserManager instance (static final)
// This MUST be shared by all ClientSession threads so that
// broadcasting and private messaging work correctly.
private static final UserManager userManager = new UserManager();
private static ServerSocket serverSocket;
public static void main(String[] args) {
// TODO: Create a ServerSocket
// TODO: In an infinite loop:
// accept an incoming client connection
// make a new thread running ClientSession for each user.
try {
serverSocket = new ServerSocket(9000);
System.out.printf("server started. port: %s\n" ,9000);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
while (true) {
Socket s = serverSocket.accept();
System.out.println("new client accepted.");
ClientSession clientSession = new ClientSession(s, userManager);
Thread thread = new Thread(clientSession);
thread.start();
}
}
catch (IOException e) {
System.err.println("Error running thread.");
}
}
}