-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
57 lines (48 loc) · 1.69 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Server implements Runnable {
private int listenport;
private ServerSocket serverSocket;
private static final int ACCEPT_TIMEOUT = 1000;
private CommunicationManager communicationManager;
private Logger logger;
public Server(int port, CommunicationManager communicationManager, Logger logger) throws UnknownHostException, IOException {
this.listenport = port;
this.serverSocket = new ServerSocket(port);
this.communicationManager = communicationManager;
this.logger = logger;
}
public void run() {
System.out.println("Start listening on port: " + this.listenport + ".");
//listen for connections from other peers
while (true) {
System.out.println("Listen for connections from other peers");
Socket socket = null;
try {
socket = serverSocket.accept();
Client newClient = new Client(socket);
Thread handlerThread = new Thread(new MessageHandler(newClient, this.communicationManager, this.logger));
handlerThread.start();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public int getListenport() {
boolean a = true;
if (!a) {
System.out.println("Get listen port");
}
return listenport;
}
public void setListenport(int listenport) {
boolean a = true;
if (!a) {
System.out.println("Set listen port");
}
this.listenport = listenport;
}
}