Repository navigation
Expand file tree
/
Copy pathWordleServer.java
More file actions
77 lines (66 loc) · 1.98 KB
/
Copy pathWordleServer.java
File metadata and controls
77 lines (66 loc) · 1.98 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.*;
import java.net.*;
import java.util.*;
public class WordleServer {
private static String IP;
private static int PORT;
private static ServerSocket server_socket;
private List<WordleClientHandler> clients;
public WordleServer(int PORT) throws IOException {
// this.IP = IP;
this.PORT = PORT;
this.init_socket();
clients = new ArrayList<WordleClientHandler>();
}
private void init_socket() throws IOException {
try {
server_socket = new ServerSocket(PORT);
} catch (IOException e) {
throw e;
}
}
private static void close_socket() throws IOException {
try {
if (server_socket != null && !server_socket.isClosed())
server_socket.close();
} catch (IOException e) {
throw e;
}
}
private void run() throws IOException {
try {
while (true) {
Socket new_client = server_socket.accept();
WordleClientHandler client_handler = new WordleClientHandler(new_client);
add_list(client_handler);
client_handler.run();
}
} catch (IOException e) {
throw e;
}
}
public void add_list(WordleClientHandler clh)
{
clients.add(clh);
}
public void remove_list(WordleClientHandler clh)
{
clients.remove(clh);
System.out.println(clients.size());
}
public static void main(String[] args) {
WordleServer server = null;
try {
server = new WordleServer(2779);
//System.out.println("Starting Server in port : " + PORT);
server.run();
} catch (IOException e) {
try {
close_socket();
} catch (IOException e1) {
System.out.println("Error: " + e1.getMessage());
}
System.out.println("Error: " + e.getMessage());
}
}
}