public void run() throws IOException { final File socketFile = new File(path); socketFile.deleteOnExit(); final ExecutorService executorService = Executors.newCachedThreadPool(); try (AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()) { server.bind(new AFUNIXSocketAddress(socketFile)); System.out.println("server: " + server); while (!Thread.interrupted()) { System.out.println("Waiting for connection..."); executorService.execute(new ClientConnection(this, server.accept())); } } finally { executorService.shutdown(); } }
public static void main(String[] args) throws IOException { final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")), "junixsocket-test.sock"); try (AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()) { server.bind(new AFUNIXSocketAddress(socketFile)); System.out.println("server: " + server); while (!Thread.interrupted()) { System.out.println("Waiting for connection..."); try (Socket sock = server.accept()) { System.out.println("Connected: " + sock); try (InputStream is = sock.getInputStream(); // OutputStream os = sock.getOutputStream()) { System.out.println("Saying hello to client " + os); os.write("Hello, dear Client".getBytes("UTF-8")); os.flush(); byte[] buf = new byte[128]; int read = is.read(buf); System.out.println("Client's response: " + new String(buf, 0, read, "UTF-8")); } } } } } }
public static void main(String[] args) throws IOException { final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")), "junixsocket-test.sock"); try (AFUNIXServerSocket server = AFUNIXServerSocket.newInstance()) { server.bind(new AFUNIXSocketAddress(socketFile)); System.out.println("server: " + server); while (!Thread.interrupted()) { System.out.println("Waiting for connection..."); try (Socket sock = server.accept()) { System.out.println("Connected: " + sock); try (InputStream is = sock.getInputStream(); OutputStream os = sock.getOutputStream()) { byte[] buf = new byte[128]; int read = is.read(buf); System.out.println("Client's response: " + new String(buf, 0, read)); System.out.println("Saying hello to client " + os); os.write("Hello, dear Client".getBytes()); os.flush(); } } } } } }