-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEchoServer
31 lines (28 loc) · 1.05 KB
/
EchoServer
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.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.net.ServerSocket;
import java.io.PrintWriter;
public class EchoServer {
private static int getPortNumber(String[] args){
if(args.length>0){
return Integer.parseInt(args[0]);
}else{
return 10001;
}
}
public static void main(String[] args) throws Exception {
// read number of the port from first argument
int port = getPortNumber(args);
// create socket
try(ServerSocket serverSocket = new ServerSocket(port)){
System.out.println("Started server on port " + port);
while (true) {
// a "blocking" call which waits until a connection is requested
try(Socket clientSocket = serverSocket.accept();PrintWriter clientSocketOut = new PrintWriter(clientSocket.getOutputStream())){
clientSocketOut.println(new SimpleDateFormat("HH:mm:ss").format(new Date()));
}
}
}
}
}