-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
44 lines (34 loc) · 1.04 KB
/
Server.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
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket serverSocket;
public Server(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public void startServer() {
try {
while (!serverSocket.isClosed()) {
Socket socket = serverSocket.accept();
ClientHandler clientHandler = new ClientHandler(socket);
Thread thread = new Thread(clientHandler);
thread.start();
}
} catch (IOException e) {
}
}
public void closeServerSocket() {
try {
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1234);
Server server = new Server(serverSocket);
server.startServer();
}
}