1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.newsclub.net.unix.demo;
19
20 import java.io.DataInputStream;
21 import java.io.DataOutputStream;
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.nio.charset.StandardCharsets;
27
28 import org.newsclub.net.unix.AFUNIXServerSocket;
29 import org.newsclub.net.unix.AFUNIXSocket;
30 import org.newsclub.net.unix.AFUNIXSocketAddress;
31 import org.newsclub.net.unix.SocketClosedException;
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @SuppressWarnings({"CatchAndPrintStackTrace" , "PMD.CognitiveComplexity"})
46 public final class SimpleTestServer {
47 private static final int MAX_NUMBER = 5;
48
49 private SimpleTestServer() {
50 throw new UnsupportedOperationException("No instances");
51 }
52
53 public static void main(String[] args) throws IOException {
54 final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")),
55 "junixsocket-test.sock");
56 System.out.println(socketFile);
57
58 try (AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()) {
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 server.bind(AFUNIXSocketAddress.of(socketFile));
81 System.out.println("server: " + server);
82
83 while (!Thread.interrupted() && !server.isClosed()) {
84 System.out.println("Waiting for connection...");
85
86 boolean remoteReady = false;
87 try (AFUNIXSocket sock = server.accept();
88 InputStream is = sock.getInputStream();
89 OutputStream os = sock.getOutputStream();
90 DataOutputStream dout = new DataOutputStream(os);
91 DataInputStream din = new DataInputStream(is);) {
92 remoteReady = true;
93 System.out.println("Connected: " + sock);
94
95
96
97
98
99
100
101 if (sock.checkConnectionClosed()) {
102 System.out.println("Peer closed socket right after connecting");
103 continue;
104 }
105
106 System.out.println("Saying hello to client " + sock);
107 os.write("Hello, dear Client".getBytes(StandardCharsets.UTF_8));
108 os.flush();
109
110 byte[] buf = new byte[128];
111 int read = is.read(buf);
112 System.out.println("Client's response: " + new String(buf, 0, read, "UTF-8"));
113
114 System.out.println("Now counting to " + MAX_NUMBER + "...");
115 int number = 0;
116 while (!Thread.interrupted()) {
117 number++;
118 System.out.println("write " + number);
119 dout.writeInt(number);
120 try {
121 Thread.sleep(1000);
122 } catch (InterruptedException e) {
123 e.printStackTrace();
124 break;
125 }
126 if (number >= MAX_NUMBER) {
127 System.out.println("write -123 (end of numbers)");
128 dout.writeInt(-123);
129 break;
130 }
131
132
133
134 int theirNumber = din.readInt();
135 System.out.println("received " + theirNumber);
136 if (theirNumber != (number * 2)) {
137 throw new IllegalStateException("Received the wrong number: " + theirNumber);
138 }
139 }
140 } catch (SocketClosedException e) {
141 if (!remoteReady) {
142
143
144 } else {
145
146 e.printStackTrace();
147 }
148 } catch (IOException e) {
149
150 e.printStackTrace();
151 }
152 }
153 } finally {
154 System.out.println("Server terminated");
155 }
156 }
157 }