-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPServer.java
25 lines (20 loc) · 992 Bytes
/
TCPServer.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
import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket;
public class TCPServer {
public static final void main(final String[] args) {
ServerSocket server; InputStream is;
Socket client;
try {
server = new ServerSocket(9999);//(*@\serverBox{1 + 2)}@*)
for (int j = 5; (--j) >= 0;) { //process only 5 clients, so I can show (*@\serverBox{5)}@*) below
client = server.accept(); //wait for incoming connection (*@\serverBox{3)}@*)
System.out.println("New connection from " + client.getRemoteSocketAddress());
is = client.getInputStream(); //get stream to read from
System.out.println("X".repeat(is.read()));//(*@\serverBox{4} + \clientBox{3})@*)
client.close(); //close connection to client
}
server.close(); //(*@\serverBox{5)}@*)
} catch (Throwable t) {
t.printStackTrace();
}
}
}