Search in sources :

Example 1 with UnixSocketAddress

use of jnr.unixsocket.UnixSocketAddress in project jetty.project by eclipse.

the class UnixSocketClient method main.

public static void main(String[] args) throws Exception {
    java.io.File path = new java.io.File("/tmp/jetty.sock");
    String data = "GET / HTTP/1.1\r\nHost: unixsock\r\n\r\n";
    UnixSocketAddress address = new UnixSocketAddress(path);
    UnixSocketChannel channel = UnixSocketChannel.open(address);
    System.out.println("connected to " + channel.getRemoteSocketAddress());
    PrintWriter w = new PrintWriter(Channels.newOutputStream(channel));
    InputStreamReader r = new InputStreamReader(Channels.newInputStream(channel));
    while (true) {
        w.print(data);
        w.flush();
        CharBuffer result = CharBuffer.allocate(4096);
        r.read(result);
        result.flip();
        System.out.println("read from server: " + result.toString());
        Thread.sleep(1000);
    }
}
Also used : UnixSocketChannel(jnr.unixsocket.UnixSocketChannel) InputStreamReader(java.io.InputStreamReader) CharBuffer(java.nio.CharBuffer) UnixSocketAddress(jnr.unixsocket.UnixSocketAddress) PrintWriter(java.io.PrintWriter)

Example 2 with UnixSocketAddress

use of jnr.unixsocket.UnixSocketAddress in project linuxtools by eclipse.

the class DefaultUnixConnectionSettingsProvider method getConnectionSettings.

@Override
public List<IDockerConnectionSettings> getConnectionSettings() {
    // $NON-NLS-1$
    final File unixSocketFile = new File("/var/run/docker.sock");
    if (unixSocketFile.exists() && unixSocketFile.canRead() && unixSocketFile.canWrite()) {
        final UnixSocketAddress address = new UnixSocketAddress(unixSocketFile);
        try (final UnixSocketChannel channel = UnixSocketChannel.open(address)) {
            // assume socket works
            final UnixSocketConnectionSettings socket = new UnixSocketConnectionSettings(DefaultDockerConnectionSettingsFinder.Defaults.DEFAULT_UNIX_SOCKET_PATH);
            socket.setName(socket.getPath());
            return Arrays.asList(socket);
        } catch (IOException e) {
        // do nothing, just assume socket did not work.
        }
    }
    return Collections.emptyList();
}
Also used : UnixSocketChannel(jnr.unixsocket.UnixSocketChannel) IOException(java.io.IOException) File(java.io.File) UnixSocketAddress(jnr.unixsocket.UnixSocketAddress)

Example 3 with UnixSocketAddress

use of jnr.unixsocket.UnixSocketAddress in project jetty.project by eclipse.

the class UnixSocketConnector method open.

public void open() throws IOException {
    if (_acceptChannel == null) {
        UnixServerSocketChannel serverChannel = UnixServerSocketChannel.open();
        SocketAddress bindAddress = new UnixSocketAddress(new File(_unixSocket));
        serverChannel.socket().bind(bindAddress, getAcceptQueueSize());
        serverChannel.configureBlocking(getAcceptors() > 0);
        addBean(serverChannel);
        LOG.debug("opened {}", serverChannel);
        _acceptChannel = serverChannel;
    }
}
Also used : SocketAddress(java.net.SocketAddress) UnixSocketAddress(jnr.unixsocket.UnixSocketAddress) File(java.io.File) UnixServerSocketChannel(jnr.unixsocket.UnixServerSocketChannel) UnixSocketAddress(jnr.unixsocket.UnixSocketAddress)

Aggregations

UnixSocketAddress (jnr.unixsocket.UnixSocketAddress)3 File (java.io.File)2 UnixSocketChannel (jnr.unixsocket.UnixSocketChannel)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 SocketAddress (java.net.SocketAddress)1 CharBuffer (java.nio.CharBuffer)1 UnixServerSocketChannel (jnr.unixsocket.UnixServerSocketChannel)1