Repository navigation
Expand file tree
/
Copy pathserver.java
More file actions
54 lines (46 loc) · 1.49 KB
/
Copy pathserver.java
File metadata and controls
54 lines (46 loc) · 1.49 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
public class server {
private static int PORT = 9090;
private static ArrayList<ClientHandler> clients = new ArrayList<>();
public static ArrayList<String> names = new ArrayList<>();
private static int maxClients = 5;
private static ExecutorService pool = Executors.newFixedThreadPool(maxClients);
private static int idNum = 0;
private static Lock lock;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ServerSocket listener = new ServerSocket(PORT);
while(true) {
System.out.println("Waiting to establish a connection to client!");
Socket client = listener.accept();
System.out.println("Established a connection to client!");
ClientHandler clientThread = new ClientHandler(client, clients, idNum);
idNum++;
clients.add(clientThread);
pool.execute(clientThread);
}
}
public static void RemoveClient(int position, String name) {
int i = 0;
for(String s : names) {
if(names.get(i) == name) {
names.remove(i);
break;
}
i++;
}
clients.remove(position);
}
public static void AddName(String name) {
names.add(name);
}
}