-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConnectManyServer.java
51 lines (46 loc) · 1.94 KB
/
ConnectManyServer.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
package com.doing.more.java.example;
import org.quickconnectfamily.json.JSONInputStream;
import org.quickconnectfamily.json.JSONOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class ConnectManyServer {
private Executor theExecutor = Executors.newCachedThreadPool();
public static void main(String[] args) {
ConnectManyServer theServer = new ConnectManyServer();
theServer.start();
}
private void start(){
try {
//a socket opened on the specified port
ServerSocket aListeningSocket =
new ServerSocket(9393);
while(true){
//wait for a connection
System.out.println("Waiting for client connection request.");
final Socket clientSocket = aListeningSocket.accept();
this.theExecutor.execute(new Runnable() {
@Override
public void run() {
try {
JSONInputStream inFromClient = new JSONInputStream(clientSocket.getInputStream());
JSONOutputStream outToClient = new JSONOutputStream(clientSocket.getOutputStream());
System.out.println("Waiting for a message from the client.");
HashMap aRequest = (HashMap) inFromClient.readObject();
System.out.println("Just got:" + aRequest + " from client");
aRequest.put("command", "Done");
outToClient.writeObject(aRequest);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}