/** * Returns a new AF_UNIX {@link ServerSocket} that is bound to the given * {@link AFUNIXSocketAddress}. * * @return The new, unbound {@link AFUNIXServerSocket}. */ public static AFUNIXServerSocket bindOn(final AFUNIXSocketAddress addr) throws IOException { AFUNIXServerSocket socket = newInstance(); socket.bind(addr); return socket; }
/** * Returns a new, unbound AF_UNIX {@link ServerSocket}. * * @return The new, unbound {@link AFUNIXServerSocket}. */ public static AFUNIXServerSocket newInstance() throws IOException { AFUNIXServerSocket instance = new AFUNIXServerSocket(); return instance; }
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 class DevLogServer { public static void main(String[] args) throws IOException { final File socketFile = new File("/dev/log"); AFUNIXServerSocket server = AFUNIXServerSocket.newInstance(); try { server.bind(new AFUNIXSocketAddress(socketFile)); } catch (Exception e) { throw e; } } }
@Override public void bind(SocketAddress endpoint, int backlog) throws IOException { if (isClosed()) { throw new SocketException("Socket is closed"); } if (isBound()) { throw new SocketException("Already bound"); } if (!(endpoint instanceof AFUNIXSocketAddress)) { throw new IOException("Can only bind to endpoints of type " + AFUNIXSocketAddress.class .getName()); } implementation.bind(backlog, endpoint); boundEndpoint = (AFUNIXSocketAddress) endpoint; }
@Override public void close() throws IOException { if (isClosed()) { return; } super.close(); implementation.close(); if (boundEndpoint != null) { NativeUnixSocket.unlink(boundEndpoint.getSocketFile()); } try { Runtime.getRuntime().removeShutdownHook(shutdownThread); } catch (IllegalStateException e) { // ignore } }
@Override public String toString() { if (!isBound()) { return "AFUNIXServerSocket[unbound]"; } return "AFUNIXServerSocket[" + boundEndpoint.getSocketFile() + "]"; }
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")); } } } } } }
@Override public void bind(SocketAddress endpoint, int backlog) throws IOException { if (isClosed()) { throw new SocketException("Socket is closed"); } if (isBound()) { throw new SocketException("Already bound"); } if (!(endpoint instanceof AFUNIXSocketAddress)) { throw new IOException("Can only bind to endpoints of type " + AFUNIXSocketAddress.class.getName()); } implementation.bind(backlog, endpoint); boundEndpoint = (AFUNIXSocketAddress) endpoint; }
@Override public void close() throws IOException { if (isClosed()) { return; } super.close(); implementation.close(); if (boundEndpoint != null) { NativeUnixSocket.unlink(boundEndpoint.getSocketFile()); } try { Runtime.getRuntime().removeShutdownHook(shutdownThread); } catch (IllegalStateException e) { // ignore } }
@Override public String toString() { if (!isBound()) { return "AFUNIXServerSocket[unbound]"; } return "AFUNIXServerSocket[" + boundEndpoint.getSocketFile() + "]"; }
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(); } } } } } }
/** * Returns a new AF_UNIX {@link ServerSocket} that is bound to the given * {@link AFUNIXSocketAddress}. * * @param addr The socket file to bind to. * @return The new, unbound {@link AFUNIXServerSocket}. * @throws IOException if the operation fails. */ public static AFUNIXServerSocket bindOn(final AFUNIXSocketAddress addr) throws IOException { AFUNIXServerSocket socket = newInstance(); socket.bind(addr); return socket; }
@Override public Socket accept() throws IOException { if (isClosed()) { throw new SocketException("Socket is closed"); } AFUNIXSocket as = AFUNIXSocket.newInstance(); implementation.accept(as.impl); as.addr = boundEndpoint; NativeUnixSocket.setConnected(as); return as; }
/** * Returns a new, unbound AF_UNIX {@link ServerSocket}. * * @return The new, unbound {@link AFUNIXServerSocket}. * @throws IOException if the operation fails. */ public static AFUNIXServerSocket newInstance() throws IOException { AFUNIXServerSocket instance = new AFUNIXServerSocket(); return instance; }
@Override public Socket accept() throws IOException { if (isClosed()) { throw new SocketException("Socket is closed"); } AFUNIXSocket as = AFUNIXSocket.newInstance(); implementation.accept(as.impl); as.addr = boundEndpoint; NativeUnixSocket.setConnected(as); return as; }