-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
37 lines (33 loc) · 1.08 KB
/
Server.java
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
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
public class Server {
private static ServerSocket listener;
public static void main(String[] args) throws Exception {
try {
Utils utils = new Utils();
//IP address
String serverAddress = utils.getIp();
// Server Port
int serverPort = utils.getPort();
// Create connection between client and server
listener = new ServerSocket();
listener.setReuseAddress(true);
InetAddress serverIp = InetAddress.getByName(serverAddress);
// Assign to listener the IP address to listen to and port
listener.bind(new InetSocketAddress(serverIp, serverPort));
System.out.println(
"Server is running : at " + serverAddress + " Port " + serverPort
);
try {
while (true) {
new ClientHandler(listener.accept(), utils).start();
}
} finally {
listener.close();
}
} catch (Exception e) {
System.out.println("Error while exiting.");
}
}
}