forked from AdvancedProgramming1404/HW-10-Socket-Programming
ChatServer completed.
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user