public static void main(String[] args) throws IOException { final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")), "junixsocket-test.sock"); AFUNIXSocket sock = AFUNIXSocket.newInstance(); try { sock.connect(new AFUNIXSocketAddress(socketFile)); } catch (AFUNIXSocketException e) { System.out.println("Cannot connect to server. Have you started it?"); System.out.flush(); throw e; } System.out.println("Connected"); InputStream is = sock.getInputStream(); OutputStream os = sock.getOutputStream(); byte[] buf = new byte[128]; int read = is.read(buf); System.out.println("Server says: " + new String(buf, 0, read)); System.out.println("Replying to server..."); os.write("Hello Server".getBytes()); os.flush(); os.close(); is.close(); sock.close(); System.out.println("End of communication."); }
public static void main(String[] args) throws IOException { final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")), "junixsocket-test.sock"); AFUNIXServerSocket server = AFUNIXServerSocket.newInstance(); server.bind(new AFUNIXSocketAddress(socketFile)); System.out.println("server: " + server); while (!Thread.interrupted()) { System.out.println("Waiting for connection..."); Socket sock = server.accept(); System.out.println("Connected: " + sock); InputStream is = sock.getInputStream(); OutputStream os = sock.getOutputStream(); System.out.println("Saying hello to client " + os); os.write("Hello, dear Client".getBytes()); os.flush(); byte[] buf = new byte[128]; int read = is.read(buf); System.out.println("Client's response: " + new String(buf, 0, read)); os.close(); is.close(); sock.close(); } }