-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
33 lines (23 loc) · 784 Bytes
/
Main.java
File metadata and controls
33 lines (23 loc) · 784 Bytes
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
package com.example.sockets;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
// obtener puerto libre
ServerSocket server = new ServerSocket(0);
int port = server.getLocalPort();
server.close();
// crear server socket
Executors.newSingleThreadExecutor().submit(() -> new Server().start(port));
Thread.sleep(1000L);
// crear client
Client client = new Client();
client.start("127.0.0.1", port);
client.send("hello");
client.send("world");
client.send("!");
client.send("exit");
client.stop();
}
}