HW-10-last-edition
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
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 int PORT = 12345;
|
||||
private static final UserManager userManager = new UserManager();
|
||||
|
||||
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.
|
||||
System.out.println("Chat server started on port " + PORT);
|
||||
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
|
||||
while (true) {
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
System.out.println("New connection from " + clientSocket.getInetAddress());
|
||||
Thread thread = new Thread(new ClientSession(clientSocket, userManager));
|
||||
thread.start();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Server error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user