<dependency> <groupId>com.github.jnr</groupId> <artifactId>jnr-unixsocket</artifactId> <version>0.21</version> </dependency> groovy implementation 'com.github.jnr:jnr-unixsocket:0.21' import java.net.SocketAddress; import jnr.unixsocket.UnixServerSocket; import jnr.unixsocket.UnixSocket; import jnr.unixsocket.UnixSocketAddress; public class UnixSocketServer { public static void main(String[] args) throws Exception { UnixServerSocket serverSocket = UnixServerSocket.newInstance(); String socketPath = "/tmp/my_unix_socket"; UnixSocketAddress socketAddress = new UnixSocketAddress(socketPath); serverSocket.bind(socketAddress); UnixSocket clientSocket = serverSocket.accept(); byte[] buffer = new byte[1024]; int bytesRead = clientSocket.getInputStream().read(buffer); clientSocket.close(); serverSocket.close(); } } import jnr.unixsocket.UnixSocket; import jnr.unixsocket.UnixSocketAddress; public class UnixSocketClient { public static void main(String[] args) throws Exception { String socketPath = "/tmp/my_unix_socket"; UnixSocketAddress socketAddress = new UnixSocketAddress(socketPath); UnixSocket clientSocket = UnixSocket.newInstance(); clientSocket.connect(socketAddress); String message = "Hello, Server!"; clientSocket.getOutputStream().write(message.getBytes()); clientSocket.close(); } }


上一篇:
下一篇:
切换中文