|
| 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