Repository navigation
Expand file tree
/
Copy pathSockets.java
More file actions
302 lines (291 loc) · 12.8 KB
/
Copy pathSockets.java
File metadata and controls
302 lines (291 loc) · 12.8 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
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.*;
import java.util.zip.CRC32;
public class Sockets {
static final InetAddress LO = InetAddress.getLoopbackAddress();
public static void main(String[] args) throws Exception {
lineEcho();
bulk();
timeoutAndClose();
refused();
options();
selectorEcho();
udp();
datagramChannel();
names();
}
static void lineEcho() throws Exception {
try (ServerSocket server = new ServerSocket(0, 50, LO)) {
Thread t = Thread.ofPlatform().start(() -> {
try (Socket s = server.accept()) {
var in = new BufferedReader(new InputStreamReader(s.getInputStream()));
var out = new PrintWriter(s.getOutputStream(), true);
String line;
while ((line = in.readLine()) != null) out.println(line.toUpperCase());
} catch (IOException e) {
System.out.println("server " + e);
}
});
try (Socket c = new Socket(LO, server.getLocalPort())) {
var in = new BufferedReader(new InputStreamReader(c.getInputStream()));
var out = new PrintWriter(c.getOutputStream(), true);
for (String w : List.of("hello", "java", "web")) {
out.println(w);
System.out.println("echo " + in.readLine());
}
System.out.println("connected " + c.isConnected() + " bound " + c.isBound());
System.out.println("same port " + (c.getPort() == server.getLocalPort()));
c.shutdownOutput();
System.out.println("eof " + in.readLine());
}
t.join();
}
}
static void bulk() throws Exception {
byte[] data = new byte[3_000_000];
new Random(7).nextBytes(data);
try (ServerSocket server = new ServerSocket(0, 50, LO)) {
CRC32 sent = new CRC32();
sent.update(data);
var received = new CompletableFuture<Long>();
Thread.ofPlatform().start(() -> {
try (Socket s = server.accept()) {
CRC32 crc = new CRC32();
byte[] buf = new byte[8192];
int n;
while ((n = s.getInputStream().read(buf)) > 0) crc.update(buf, 0, n);
received.complete(crc.getValue());
s.getOutputStream().write(1);
} catch (IOException e) {
received.completeExceptionally(e);
}
});
try (Socket c = new Socket(LO, server.getLocalPort())) {
c.getOutputStream().write(data);
c.shutdownOutput();
System.out.println("ack " + c.getInputStream().read());
}
System.out.println("bulk ok " + (received.get() == sent.getValue()));
}
}
static void timeoutAndClose() throws Exception {
try (ServerSocket server = new ServerSocket(0, 50, LO)) {
server.setSoTimeout(100);
try {
server.accept();
} catch (SocketTimeoutException e) {
System.out.println("accept " + e.getMessage());
}
server.setSoTimeout(0);
Thread closer = Thread.ofPlatform().start(() -> {
try {
Thread.sleep(100);
server.close();
} catch (Exception e) {
System.out.println(e);
}
});
try {
server.accept();
} catch (SocketException e) {
System.out.println("accept " + e.getMessage());
}
closer.join();
}
try (ServerSocket server = new ServerSocket(0, 50, LO); Socket c = new Socket(LO, server.getLocalPort())) {
c.setSoTimeout(100);
try {
c.getInputStream().read();
} catch (SocketTimeoutException e) {
System.out.println("read " + e.getMessage());
}
Socket peer = server.accept();
Thread closer = Thread.ofPlatform().start(() -> {
try {
Thread.sleep(100);
c.close();
} catch (Exception e) {
System.out.println(e);
}
});
c.setSoTimeout(0);
try {
c.getInputStream().read();
} catch (SocketException e) {
System.out.println("read " + e.getMessage());
}
closer.join();
System.out.println("peer eof " + peer.getInputStream().read());
peer.close();
}
}
static void refused() throws Exception {
int port;
try (ServerSocket s = new ServerSocket(0, 50, LO)) {
port = s.getLocalPort();
}
try (Socket c = new Socket()) {
c.connect(new InetSocketAddress(LO, port));
} catch (ConnectException e) {
System.out.println(e);
}
try (Socket c = new Socket()) {
c.connect(new InetSocketAddress(LO, port), 2000);
} catch (ConnectException e) {
System.out.println(e);
}
try (ServerSocket a = new ServerSocket(0, 50, LO)) {
try (ServerSocket b = new ServerSocket(a.getLocalPort(), 50, LO)) {
System.out.println("bound twice");
} catch (BindException e) {
System.out.println(e);
}
}
}
static void options() throws Exception {
try (ServerSocket server = new ServerSocket(0, 50, LO); Socket c = new Socket(LO, server.getLocalPort())) {
c.setTcpNoDelay(true);
c.setKeepAlive(true);
c.setSoLinger(true, 5);
c.setSendBufferSize(65536);
c.setReceiveBufferSize(65536);
System.out.println("nodelay " + c.getTcpNoDelay() + " keepalive " + c.getKeepAlive());
System.out.println("linger " + c.getSoLinger());
System.out.println("buffers " + (c.getSendBufferSize() >= 65536) + " " + (c.getReceiveBufferSize() >= 65536));
System.out.println("reuse " + server.getReuseAddress());
System.out.println("local " + c.getLocalAddress().isLoopbackAddress());
}
}
static void selectorEcho() throws Exception {
try (Selector sel = Selector.open(); ServerSocketChannel ssc = ServerSocketChannel.open()) {
ssc.bind(new InetSocketAddress(LO, 0));
ssc.configureBlocking(false);
ssc.register(sel, SelectionKey.OP_ACCEPT);
int port = ((InetSocketAddress) ssc.getLocalAddress()).getPort();
Thread client = Thread.ofPlatform().start(() -> {
try (SocketChannel sc = SocketChannel.open(new InetSocketAddress(LO, port))) {
for (String m : List.of("one", "two", "three")) {
sc.write(ByteBuffer.wrap(m.getBytes(StandardCharsets.UTF_8)));
ByteBuffer b = ByteBuffer.allocate(16);
while (b.position() < m.length()) if (sc.read(b) < 0) break;
System.out.println("client got " + new String(b.array(), 0, b.position(), StandardCharsets.UTF_8));
}
} catch (IOException e) {
System.out.println("client " + e);
}
});
int accepted = 0, closed = 0;
while (closed == 0) {
sel.select();
for (var it = sel.selectedKeys().iterator(); it.hasNext(); ) {
SelectionKey k = it.next();
it.remove();
if (k.isAcceptable()) {
SocketChannel ch = ssc.accept();
ch.configureBlocking(false);
ch.register(sel, SelectionKey.OP_READ);
accepted++;
} else if (k.isReadable()) {
SocketChannel ch = (SocketChannel) k.channel();
ByteBuffer b = ByteBuffer.allocate(16);
int n = ch.read(b);
if (n < 0) {
ch.close();
closed++;
} else {
b.flip();
while (b.hasRemaining()) ch.write(b);
}
}
}
}
client.join();
System.out.println("accepted " + accepted + " closed " + closed);
Thread waker = Thread.ofPlatform().start(() -> {
try {
Thread.sleep(100);
sel.wakeup();
} catch (InterruptedException e) {
}
});
System.out.println("select after wakeup " + sel.select());
waker.join();
System.out.println("select timeout " + sel.select(50));
}
}
static void udp() throws Exception {
try (DatagramSocket server = new DatagramSocket(0, LO); DatagramSocket c = new DatagramSocket(0, LO)) {
Thread t = Thread.ofPlatform().start(() -> {
try {
byte[] buf = new byte[100];
for (int i = 0; i < 2; i++) {
DatagramPacket p = new DatagramPacket(buf, buf.length);
server.receive(p);
String s = new String(p.getData(), 0, p.getLength(), StandardCharsets.UTF_8);
byte[] r = s.toUpperCase().getBytes(StandardCharsets.UTF_8);
server.send(new DatagramPacket(r, r.length, p.getSocketAddress()));
}
} catch (IOException e) {
System.out.println("server " + e);
}
});
for (String m : List.of("ping", "pong")) {
byte[] b = m.getBytes(StandardCharsets.UTF_8);
c.send(new DatagramPacket(b, b.length, LO, server.getLocalPort()));
DatagramPacket p = new DatagramPacket(new byte[100], 100);
c.setSoTimeout(5000);
c.receive(p);
System.out.println("udp " + new String(p.getData(), 0, p.getLength(), StandardCharsets.UTF_8)
+ " from server " + (p.getPort() == server.getLocalPort()));
}
t.join();
c.setSoTimeout(100);
try {
c.receive(new DatagramPacket(new byte[10], 10));
} catch (SocketTimeoutException e) {
System.out.println("udp " + e.getMessage());
}
byte[] small = "truncated".getBytes(StandardCharsets.UTF_8);
server.send(new DatagramPacket(small, small.length, LO, c.getLocalPort()));
DatagramPacket p = new DatagramPacket(new byte[4], 4);
c.receive(p);
System.out.println("udp " + new String(p.getData(), 0, p.getLength(), StandardCharsets.UTF_8));
}
}
static void datagramChannel() throws Exception {
try (DatagramChannel a = DatagramChannel.open(); DatagramChannel b = DatagramChannel.open()) {
a.bind(new InetSocketAddress(LO, 0));
b.bind(new InetSocketAddress(LO, 0));
b.connect(a.getLocalAddress());
a.connect(b.getLocalAddress());
b.write(ByteBuffer.wrap("connected".getBytes(StandardCharsets.UTF_8)));
ByteBuffer buf = ByteBuffer.allocate(32);
a.read(buf);
System.out.println("channel " + new String(buf.array(), 0, buf.position(), StandardCharsets.UTF_8));
a.configureBlocking(false);
buf.clear();
System.out.println("nothing " + a.read(buf));
a.disconnect();
System.out.println("connected " + a.isConnected());
}
}
static void names() throws Exception {
System.out.println(InetAddress.getByName("localhost"));
System.out.println(InetAddress.getByName("127.0.0.1").isLoopbackAddress());
System.out.println(InetAddress.getByAddress(new byte[] {10, 1, 2, 3}).getHostAddress());
System.out.println(InetAddress.getLoopbackAddress());
var lo = NetworkInterface.getByInetAddress(LO);
System.out.println(lo.isLoopback() + " " + lo.isUp() + " " + lo.inetAddresses().anyMatch(LO::equals));
try {
InetAddress.getByName("no-such-host.invalid");
} catch (UnknownHostException e) {
System.out.println("unknown host");
}
System.out.println(InetAddress.getLocalHost() != null);
}
}