Repository navigation
Expand file tree
/
Copy pathSocketEdges.java
More file actions
275 lines (261 loc) · 11.2 KB
/
Copy pathSocketEdges.java
File metadata and controls
275 lines (261 loc) · 11.2 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.*;
public class SocketEdges {
static final InetAddress LO = InetAddress.getLoopbackAddress();
public static void main(String[] args) throws Exception {
connectWithSelector();
virtualThreads();
interruptedAccept();
resetAndBrokenPipe();
availableAndShutdown();
multicastSocket();
reachable();
wildcardBindConnects();
listenerCloseResets();
datagramQueueFull();
reusePort();
datagramErrors();
}
static void connectWithSelector() throws Exception {
try (ServerSocketChannel ssc = ServerSocketChannel.open().bind(new InetSocketAddress(LO, 0));
Selector sel = Selector.open();
SocketChannel sc = SocketChannel.open()) {
sc.configureBlocking(false);
boolean now = sc.connect(ssc.getLocalAddress());
System.out.println("pending " + sc.isConnectionPending() + " immediate " + now);
sc.register(sel, SelectionKey.OP_CONNECT);
while (!sc.isConnected()) {
sel.select(1000);
for (var k : sel.selectedKeys()) if (k.isConnectable()) System.out.println("finish " + sc.finishConnect());
sel.selectedKeys().clear();
}
SocketChannel peer = ssc.accept();
sc.keyFor(sel).interestOps(SelectionKey.OP_WRITE);
sel.select(1000);
System.out.println("writable " + sel.selectedKeys().iterator().next().isWritable());
peer.close();
sc.keyFor(sel).interestOps(SelectionKey.OP_READ);
sel.selectedKeys().clear();
sel.select(1000);
System.out.println("readable at eof " + sc.read(ByteBuffer.allocate(4)));
}
try (SocketChannel sc = SocketChannel.open()) {
sc.configureBlocking(false);
int port;
try (ServerSocket s = new ServerSocket(0, 1, LO)) {
port = s.getLocalPort();
}
try {
if (!sc.connect(new InetSocketAddress(LO, port))) {
while (!sc.finishConnect()) Thread.sleep(10);
}
} catch (ConnectException e) {
System.out.println(e);
}
}
}
static void virtualThreads() throws Exception {
try (ServerSocket server = new ServerSocket(0, 50, LO)) {
var done = new CompletableFuture<String>();
Thread.ofVirtual().start(() -> {
try (Socket s = server.accept()) {
s.getOutputStream().write(s.getInputStream().readNBytes(5));
} catch (IOException e) {
done.completeExceptionally(e);
}
});
Thread.ofVirtual().start(() -> {
try (Socket c = new Socket(LO, server.getLocalPort())) {
c.getOutputStream().write("hello".getBytes());
done.complete(new String(c.getInputStream().readNBytes(5)));
} catch (IOException e) {
done.completeExceptionally(e);
}
});
// a fresh VM starved of CPU takes tens of seconds over its first socket use
System.out.println("virtual " + done.get(120, TimeUnit.SECONDS));
}
}
static void interruptedAccept() throws Exception {
try (ServerSocketChannel ssc = ServerSocketChannel.open().bind(new InetSocketAddress(LO, 0))) {
Thread t = Thread.currentThread();
Thread.ofPlatform().start(() -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
t.interrupt();
});
try {
ssc.accept();
} catch (ClosedByInterruptException e) {
System.out.println("interrupted " + Thread.interrupted());
}
}
}
static void resetAndBrokenPipe() throws Exception {
try (ServerSocket server = new ServerSocket(0, 50, LO)) {
Socket c = new Socket(LO, server.getLocalPort());
Socket peer = server.accept();
peer.setSoLinger(true, 0);
peer.close();
Thread.sleep(100);
try {
c.getInputStream().read();
System.out.println("no reset");
} catch (SocketException e) {
System.out.println(e.getMessage());
}
c.close();
}
try (ServerSocket server = new ServerSocket(0, 50, LO)) {
Socket c = new Socket(LO, server.getLocalPort());
server.accept().close();
Thread.sleep(100);
try {
byte[] big = new byte[1 << 16];
for (int i = 0; i < 100; i++) c.getOutputStream().write(big);
System.out.println("wrote");
} catch (SocketException e) {
System.out.println("write " + e.getMessage());
}
c.close();
}
}
static void availableAndShutdown() throws Exception {
try (ServerSocket server = new ServerSocket(0, 50, LO); Socket c = new Socket(LO, server.getLocalPort())) {
Socket peer = server.accept();
peer.getOutputStream().write(new byte[100]);
Thread.sleep(100);
System.out.println("available " + c.getInputStream().available());
InputStream in = c.getInputStream();
c.shutdownInput();
System.out.println("after shutdownInput " + in.read() + " " + c.isInputShutdown());
c.shutdownOutput();
System.out.println("peer sees " + peer.getInputStream().read());
try {
c.getOutputStream().write(1);
} catch (SocketException e) {
System.out.println(e.getMessage());
}
peer.close();
}
}
static void multicastSocket() throws Exception {
try (MulticastSocket s = new MulticastSocket(0)) {
System.out.println("ttl " + s.getTimeToLive() + " loopback-disabled " + s.getLoopbackMode());
s.setTimeToLive(3);
System.out.println("ttl " + s.getTimeToLive());
}
}
static void reachable() throws Exception {
System.out.println(LO.isReachable(100));
System.out.println(Arrays.stream(InetAddress.getAllByName("localhost")).anyMatch(InetAddress::isLoopbackAddress));
System.out.println(InetAddress.getByName("127.0.0.1").getHostName());
}
static void wildcardBindConnects() throws Exception {
try (ServerSocket server = new ServerSocket(0, 50, LO); Socket c = new Socket()) {
c.bind(null);
c.connect(new InetSocketAddress(LO, server.getLocalPort()));
try (Socket peer = server.accept()) {
System.out.println("local " + c.getLocalAddress() + " remote " + peer.getInetAddress());
}
}
}
static void listenerCloseResets() throws Exception {
ServerSocket server = new ServerSocket(0, 50, LO);
try (Socket c = new Socket(LO, server.getLocalPort())) {
server.close();
try {
c.getInputStream().read();
System.out.println("no reset");
} catch (SocketException e) {
System.out.println("listener closed: " + e.getMessage());
}
}
}
// Past its receive buffer a socket drops datagrams, and frees the room as they are read.
static void datagramQueueFull() throws Exception {
try (DatagramSocket rx = new DatagramSocket(0, LO); DatagramSocket tx = new DatagramSocket(0, LO)) {
byte[] big = new byte[60000];
for (int i = 0; i < 100; i++) tx.send(new DatagramPacket(big, big.length, LO, rx.getLocalPort()));
rx.setSoTimeout(500);
DatagramPacket p = new DatagramPacket(new byte[60000], 60000);
int got = 0;
try {
while (true) {
rx.receive(p);
got++;
}
} catch (SocketTimeoutException e) {
}
System.out.println("some dropped " + (got > 0 && got < 100));
tx.send(new DatagramPacket(new byte[10], 10, LO, rx.getLocalPort()));
rx.receive(p);
System.out.println("after drain " + p.getLength());
}
}
static void reusePort() throws Exception {
for (boolean both : new boolean[] {true, false}) {
try (ServerSocketChannel a = ServerSocketChannel.open(); ServerSocketChannel b = ServerSocketChannel.open()) {
a.setOption(StandardSocketOptions.SO_REUSEPORT, true);
b.setOption(StandardSocketOptions.SO_REUSEPORT, both);
a.bind(new InetSocketAddress(LO, 0));
b.bind(a.getLocalAddress());
System.out.println("tcp reuseport " + both + " bound");
} catch (BindException e) {
System.out.println("tcp reuseport " + both + " " + e.getMessage());
}
try (DatagramChannel a = DatagramChannel.open(); DatagramChannel b = DatagramChannel.open()) {
a.setOption(StandardSocketOptions.SO_REUSEPORT, true);
b.setOption(StandardSocketOptions.SO_REUSEPORT, both);
a.bind(new InetSocketAddress(LO, 0));
b.bind(a.getLocalAddress());
System.out.println("udp reuseport " + both + " bound");
} catch (BindException e) {
System.out.println("udp reuseport " + both + " " + e.getMessage());
}
}
}
static void datagramErrors() throws Exception {
int closedPort;
try (DatagramSocket tmp = new DatagramSocket(0, LO)) {
closedPort = tmp.getLocalPort();
}
DatagramPacket p = new DatagramPacket(new byte[10], 10);
try (DatagramSocket ds = new DatagramSocket(0, LO)) {
ds.setSoTimeout(1000);
ds.send(new DatagramPacket(new byte[1], 1, LO, closedPort));
try {
ds.receive(p);
} catch (SocketTimeoutException e) {
System.out.println("unconnected " + e.getMessage());
}
ds.connect(LO, closedPort);
ds.send(new DatagramPacket(new byte[1], 1));
try {
ds.receive(p);
} catch (PortUnreachableException e) {
System.out.println("connected " + e.getClass().getName());
}
try {
ds.send(new DatagramPacket(new byte[65508], 65508));
} catch (IOException e) {
System.out.println(e.getClass().getName() + ": " + e.getMessage());
}
}
try (DatagramSocket ds = new DatagramSocket(0, LO)) {
ds.send(new DatagramPacket(new byte[65507], 65507, LO, closedPort));
try {
ds.send(new DatagramPacket(new byte[65508], 65508, LO, closedPort));
} catch (IOException e) {
System.out.println(e.getClass().getName() + ": " + e.getMessage());
}
}
}
}