-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEchoTcpServer.java
29 lines (28 loc) · 1.11 KB
/
EchoTcpServer.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
/**
* Simple tcp server that print everything that goes through a tcp socket
*/
class EchoTcpServer {
public static void main(String[] args) {
System.initializeSystemClass(); // Mandatory call
if (args.length < 1) {
System.out.println("Usage: EchoTcpServer <listen_port>");
return;
}
int portNumber = Integer.parseInt(args[0]);
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
FileInputStream is = clientSocket.getInputStream();
int l = 1;
String s;
char[] buffer = new char[16];
char[] sub_buffer;
while (l > 0) {
l = is.read(buffer);
sub_buffer = System.arraycopy(buffer, l);
s = new String(sub_buffer);
System.out.print(s);
}
clientSocket.close();
serverSocket.close();
}
}