-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeerStub.java
More file actions
31 lines (26 loc) · 900 Bytes
/
PeerStub.java
File metadata and controls
31 lines (26 loc) · 900 Bytes
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
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.lang.Thread;
public class PeerStub implements Runnable {
private Peer peer;
public PeerStub(Peer peer) {
this.peer = peer;
}
public void run () {
try {
// Listen for new messages
ServerSocket server = new ServerSocket(peer.getPort());
System.out.println("Peer " + peer.getCredentials().get("username") + ": Listening at port " + peer.getPort() + ".");
Thread handler;
while (true) {
// Receive a new connection and start a new handler
Socket socket = server.accept();
handler = new Thread(new PeerInputHandler(peer, socket));
handler.start();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}