feat: complete TODO tasks in ChatServer.java
This commit is contained in:
@@ -1,15 +1,37 @@
|
|||||||
package com.university.chat.Server;
|
package com.university.chat.Server;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
public class ChatServer {
|
public class ChatServer {
|
||||||
// TODO: declare a single shared UserManager instance (static final)
|
|
||||||
// This MUST be shared by all ClientSession threads so that
|
private static final UserManager userManager = new UserManager();
|
||||||
// broadcasting and private messaging work correctly.
|
private static ServerSocket serverSocket;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO: Create a ServerSocket
|
|
||||||
|
|
||||||
// TODO: In an infinite loop:
|
try {
|
||||||
// accept an incoming client connection
|
serverSocket = new ServerSocket(9000);
|
||||||
// make a new thread running ClientSession for each user.
|
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.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user