-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
39 lines (32 loc) · 1.25 KB
/
Server.java
File metadata and controls
39 lines (32 loc) · 1.25 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
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private final ServerSocket serverSocket;
public Server(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public void startServer() {
try {
// Infinite loop: Keep the server running forever
while (!serverSocket.isClosed()) {
// 1. Wait for a new guest
Socket socket = serverSocket.accept();
System.out.println("A new client has connected!");
// 2. Create a handler for them
ClientHandler clientHandler = new ClientHandler(socket);
// 3. Start that handler in a separate Thread
// This allows the Server loop to go back to line 1 immediately!
Thread thread = new Thread(clientHandler);
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1234); // Port 1234
Server server = new Server(serverSocket);
server.startServer();
}
}