-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateServerSocket.java
More file actions
56 lines (47 loc) · 1.55 KB
/
Copy pathCreateServerSocket.java
File metadata and controls
56 lines (47 loc) · 1.55 KB
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
52
53
54
55
56
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CreateServerSocket implements Runnable {
private int port;
private boolean running;
private ServerSocket ss = null;
private Map<Integer, List<Socket>> clientMap = new HashMap<>();
public CreateServerSocket(int port) {
this.port = port;
}
public void start() {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
}
running = true;
new Thread(this).start();
}
@Override
public void run() {
try {
while (true) {
Socket socket = ss.accept();
if (clientMap.get(port) != null) {
List<Socket> list = clientMap.get(port);
list.add(socket);
clientMap.put(port, list);
} else {
List<Socket> list = new ArrayList<>();
list.add(socket);
clientMap.put(port, list);
}
System.out.println("accepted at port " + port + socket);
//start another client thread for each server
(new ServerClientThread(socket, clientMap.get(port))).start();
}
} catch(IOException e){
}
}
}