diff --git a/src/main/java/com/university/chat/Server/ChatServer.java b/src/main/java/com/university/chat/Server/ChatServer.java index b2a35ae..300ab7e 100644 --- a/src/main/java/com/university/chat/Server/ChatServer.java +++ b/src/main/java/com/university/chat/Server/ChatServer.java @@ -1,15 +1,30 @@ 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. + public static final UserManager userManager = new UserManager(); public static void main(String[] args) { - // TODO: Create a ServerSocket + int port = 8080; - // TODO: In an infinite loop: - // accept an incoming client connection - // make a new thread running ClientSession for each user. + try (ServerSocket serverSocket = new ServerSocket(port)) { + //for catching multiple clients at the same time + while (true) { + //waiting for the client and catches it when it connects + Socket socket = serverSocket.accept(); + + //building a client session to get the socket + ClientSession clientSession = new ClientSession(socket, userManager); + + //building a thread to this client + Thread SoketThread = new Thread(clientSession); + SoketThread.start(); + } + } catch (IOException e) { + System.out.println(e.getMessage()); + } } }