implement chatServer class
This commit is contained in:
@@ -1,15 +1,26 @@
|
||||
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 void main(String[] args) {
|
||||
// TODO: Create a ServerSocket
|
||||
private static final UserManager userManager = new UserManager();
|
||||
private static final int port = 9000;
|
||||
|
||||
// TODO: In an infinite loop:
|
||||
// accept an incoming client connection
|
||||
// make a new thread running ClientSession for each user.
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
try( ServerSocket serverSocket = new ServerSocket(port)){
|
||||
System.out.println("Server started on port " + port);
|
||||
while(true){
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
Thread clientThread = new Thread(new ClientSession(clientSocket,userManager));
|
||||
clientThread.start();
|
||||
}
|
||||
}
|
||||
catch (IOException e){
|
||||
System.out.println("server error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ public class ClientSession implements Runnable {
|
||||
public ClientSession(Socket socket, UserManager userManager) {
|
||||
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
||||
// and an ObjectInputStream from socket.getInputStream().
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user