Skip to content

Commit 73dadb5

Browse files
committed
Java 16: added example of Unix-Domain socket
1 parent 3aabc22 commit 73dadb5

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ A project to explore more about the new features of Java 8, 9, ...
1717
* Records (standard)
1818
* Pattern matching for `instanceof` (standard)
1919
* Sealed classes (preview 2)
20+
* Unix-Domain Socket Channels
2021
* Warnings for Value-Based Classes
2122
* Foreign-Memory Access API (incubator)
2223
* Vector API (Incubator)

java-16/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ To run each example use: `java --enable-preview --source 16 <FileName.java>`
1818
* change in the API
1919
* Sealed classes (second preview)
2020
* `sealed`, `non-sealed` and `permits` became contextual keywords (we can use it as var name or method name)
21+
* Unix-Domain Socket Channels
2122

2223
### JVM
2324

java-16/UnixDomainSocketTest.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.io.IOException;
2+
import java.net.StandardProtocolFamily;
3+
import java.net.UnixDomainSocketAddress;
4+
import java.nio.channels.*;
5+
import java.nio.*;
6+
import java.nio.file.*;
7+
import java.rmi.server.SocketSecurityException;
8+
9+
public class UnixDomainSocketTest {
10+
private static final String SOCKET_PATH = "server.socket";
11+
12+
public static void main(String[] args) throws Exception {
13+
var socketFile = Path.of(System.getProperty("user.home")).resolve(SOCKET_PATH);
14+
var address = UnixDomainSocketAddress.of(socketFile);
15+
16+
Thread.sleep(500);
17+
new Thread(new ServerProcess(address)).start();
18+
19+
Thread.sleep(1000);
20+
new Thread(new ClientProcess(address)).start();
21+
22+
Thread.currentThread().join(10_000);
23+
}
24+
}
25+
26+
class ServerProcess implements Runnable {
27+
private final UnixDomainSocketAddress address;
28+
29+
ServerProcess(UnixDomainSocketAddress address) {
30+
this.address = address;
31+
}
32+
33+
public void run() {
34+
try (ServerSocketChannel server = ServerSocketChannel.open(StandardProtocolFamily.UNIX)) {
35+
server.bind(this.address);
36+
37+
System.out.println("[Server] starting");
38+
SocketChannel channel = server.accept();
39+
40+
System.out.println("[Server] sending message");
41+
channel.write(ByteBuffer.wrap("Hello client!".getBytes()));
42+
43+
var buffer = ByteBuffer.allocate(1024);
44+
var length = channel.read(buffer);
45+
if (length > 0) {
46+
System.out.println("[Server] message from client: " + new String(buffer.array()));
47+
} else {
48+
System.out.println("[Server] empty");
49+
}
50+
channel.close();
51+
} catch (IOException ex) {
52+
ex.printStackTrace();
53+
}
54+
}
55+
}
56+
57+
class ClientProcess implements Runnable {
58+
private final UnixDomainSocketAddress address;
59+
60+
ClientProcess(UnixDomainSocketAddress address) {
61+
this.address = address;
62+
}
63+
64+
public void run() {
65+
try (SocketChannel client = SocketChannel.open(StandardProtocolFamily.UNIX)) {
66+
System.out.println("[Client] starting");
67+
client.connect(this.address);
68+
69+
System.out.println("[Client] reading message");
70+
var buffer = ByteBuffer.allocate(1024);
71+
var length = client.read(buffer);
72+
if (length > 0) {
73+
System.out.println("[Client] message from the server: " + new String(buffer.array()));
74+
} else {
75+
System.out.println("[Client] empty");
76+
}
77+
System.out.println("[Client] sending message");
78+
client.write(ByteBuffer.wrap("Hello server, I'm your new client!".getBytes()));
79+
} catch (IOException ex) {
80+
ex.printStackTrace();
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)