-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
54 lines (39 loc) · 1.12 KB
/
Server.java
File metadata and controls
54 lines (39 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
private int portNumber = 3000;
private List<ClientHandler> allClients;
public static void main(String[] args) {
Server server = new Server();
server.go();
}
public List<ClientHandler> getClients() {
return allClients;
}
public void go() {
try{
ServerSocket serverSocket = new ServerSocket(portNumber);
System.out.println("Server has started on port: " + portNumber);
acceptClient(serverSocket);
} catch(IOException ex) {
System.err.println("Could not listen on port: "+ portNumber);
System.exit(1);
}
} // close go
public void acceptClient(ServerSocket serverSocket){
allClients = new ArrayList<ClientHandler>();
while(true){
try{
Socket clientSocket = serverSocket.accept();
System.out.println("accepts : " + clientSocket.getRemoteSocketAddress());
ClientHandler clientThread = new ClientHandler(this, clientSocket);
Thread thread = new Thread(clientThread);
thread.start();
allClients.add(clientThread);
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}