Skip to content

Commit d621d21

Browse files
committed
Improve Java networking code snippets
1 parent cd09571 commit d621d21

File tree

1 file changed

+2
-7
lines changed
  • 11-network-ii/snippets/echoclientserver/src/bg/sofia/uni/fmi/mjt/echo/net/multithreaded

1 file changed

+2
-7
lines changed

11-network-ii/snippets/echoclientserver/src/bg/sofia/uni/fmi/mjt/echo/net/multithreaded/EchoServer.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@ public class EchoServer {
1212
private static final int MAX_EXECUTOR_THREADS = 10;
1313

1414
public static void main(String[] args) {
15-
16-
ExecutorService executor = Executors.newFixedThreadPool(MAX_EXECUTOR_THREADS);
17-
1815
Thread.currentThread().setName("Echo Server Thread");
1916

20-
try (ServerSocket serverSocket = new ServerSocket(SERVER_PORT)) {
17+
try (ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
18+
ExecutorService executor = Executors.newFixedThreadPool(MAX_EXECUTOR_THREADS);) {
2119

2220
System.out.println("Server started and listening for connect requests");
23-
2421
Socket clientSocket;
2522

2623
while (true) {
27-
2824
// Calling accept() blocks and waits for connection request by a client
2925
// When a request comes, accept() returns a socket to communicate with this
3026
// client
@@ -40,7 +36,6 @@ public static void main(String[] args) {
4036
// new Thread(clientHandler).start();
4137
executor.execute(clientHandler); // use a thread pool to launch a thread
4238
}
43-
4439
} catch (IOException e) {
4540
throw new RuntimeException("There is a problem with the server socket", e);
4641
}

0 commit comments

Comments
 (0)