Search in sources :

Example 1 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project jetty.project by eclipse.

the class IOTest method testHalfCloseClientServer.

@Test
public void testHalfCloseClientServer() throws Exception {
    ServerSocketChannel connector = ServerSocketChannel.open();
    connector.socket().bind(null);
    Socket client = SocketChannel.open(connector.socket().getLocalSocketAddress()).socket();
    client.setSoTimeout(1000);
    client.setSoLinger(false, -1);
    Socket server = connector.accept().socket();
    server.setSoTimeout(1000);
    server.setSoLinger(false, -1);
    // Write from client to server
    client.getOutputStream().write(1);
    // Server reads
    assertEquals(1, server.getInputStream().read());
    // Write from server to client with oshut
    server.getOutputStream().write(1);
    // System.err.println("OSHUT "+server);
    server.shutdownOutput();
    // Client reads response
    assertEquals(1, client.getInputStream().read());
    try {
        // Client reads -1 and does ishut
        assertEquals(-1, client.getInputStream().read());
        assertFalse(client.isInputShutdown());
        //System.err.println("ISHUT "+client);
        client.shutdownInput();
        // Client ???
        //System.err.println("OSHUT "+client);
        client.shutdownOutput();
        //System.err.println("CLOSE "+client);
        client.close();
        // Server reads -1, does ishut and then close
        assertEquals(-1, server.getInputStream().read());
        assertFalse(server.isInputShutdown());
        try {
            server.shutdownInput();
        } catch (SocketException e) {
        // System.err.println(e);
        }
        //System.err.println("CLOSE "+server);
        server.close();
    } catch (Exception e) {
        System.err.println(e);
        assertTrue(OS.IS_OSX);
    }
}
Also used : SocketException(java.net.SocketException) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) SocketException(java.net.SocketException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project jetty.project by eclipse.

the class ServerConnector method accept.

@Override
public void accept(int acceptorID) throws IOException {
    ServerSocketChannel serverChannel = _acceptChannel;
    if (serverChannel != null && serverChannel.isOpen()) {
        SocketChannel channel = serverChannel.accept();
        accepted(channel);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 3 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project cryptomator by cryptomator.

the class SingleInstanceManager method startLocalInstance.

/**
	 * Creates a server socket on a free port and saves the port in
	 * {@link Preferences#userNodeForPackage(Class)} for {@link Cryptomator} under the
	 * given applicationKey.
	 * 
	 * @param applicationKey
	 *            key used to save the port and identify upon connection.
	 * @param exec
	 *            the task which is submitted is interruptable.
	 * @return
	 * @throws IOException
	 */
public static LocalInstance startLocalInstance(String applicationKey, ExecutorService exec) throws IOException {
    final ServerSocketChannel channel = ServerSocketChannel.open();
    boolean success = false;
    try {
        channel.configureBlocking(false);
        channel.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
        final int port = ((InetSocketAddress) channel.getLocalAddress()).getPort();
        Preferences.userNodeForPackage(Cryptomator.class).putInt(applicationKey, port);
        LOG.debug("InstanceManager bound to port {}", port);
        Selector selector = Selector.open();
        channel.register(selector, SelectionKey.OP_ACCEPT);
        LocalInstance instance = new LocalInstance(applicationKey, channel, selector, port);
        exec.submit(instance::selectionLoop);
        success = true;
        return instance;
    } finally {
        if (!success) {
            channel.close();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Cryptomator(org.cryptomator.ui.Cryptomator) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 4 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project neo4j by neo4j.

the class NettyServerTest method shouldGivePortConflictErrorWithPortNumberInIt.

@Test
public void shouldGivePortConflictErrorWithPortNumberInIt() throws Throwable {
    // Given an occupied port
    int port = 16000;
    try (ServerSocketChannel ignore = ServerSocketChannel.open().bind(new InetSocketAddress("localhost", port))) {
        final ListenSocketAddress address = new ListenSocketAddress("localhost", port);
        // Expect
        exception.expect(PortBindException.class);
        exception.expectMessage("Address localhost:16000 is already in use");
        // When
        new NettyServer(new NamedThreadFactory("mythreads"), asList(protocolOnAddress(address))).start();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NamedThreadFactory(org.neo4j.helpers.NamedThreadFactory) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel) NettyServer(org.neo4j.bolt.transport.NettyServer) Test(org.junit.Test)

Example 5 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project voltdb by VoltDB.

the class SocketJoiner method doBind.

/*
     * Bind to the internal interface if one was specified,
     * otherwise bind on all interfaces. The leader won't invoke this.
     */
private void doBind() throws Exception {
    LOG.debug("Creating listener socket");
    try {
        m_selector = Selector.open();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ServerSocketChannel listenerSocket = ServerSocketChannel.open();
    InetSocketAddress inetsockaddr;
    if ((m_internalInterface == null) || (m_internalInterface.length() == 0)) {
        inetsockaddr = new InetSocketAddress(m_internalPort);
    } else {
        inetsockaddr = new InetSocketAddress(m_internalInterface, m_internalPort);
    }
    try {
        hostLog.info("Attempting to bind to internal ip " + inetsockaddr);
        listenerSocket.socket().bind(inetsockaddr);
        listenerSocket.configureBlocking(false);
        m_listenerSockets.add(listenerSocket);
    } catch (Exception e) {
        /*
             * If we bound to the leader address, the internal interface address  might not
             * bind if it is all interfaces
             */
        if (m_listenerSockets.isEmpty()) {
            LOG.fatal("Failed to bind to " + inetsockaddr);
            CoreUtils.printPortsInUse(hostLog);
            throw e;
        }
    }
    for (ServerSocketChannel ssc : m_listenerSockets) {
        ssc.register(m_selector, SelectionKey.OP_ACCEPT);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Non-Primary Listening on:" + inetsockaddr.toString());
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) ServerSocketChannel(java.nio.channels.ServerSocketChannel) IOException(java.io.IOException) EOFException(java.io.EOFException) JSONException(org.json_voltpatches.JSONException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Aggregations

ServerSocketChannel (java.nio.channels.ServerSocketChannel)274 SocketChannel (java.nio.channels.SocketChannel)156 InetSocketAddress (java.net.InetSocketAddress)128 IOException (java.io.IOException)96 ByteBuffer (java.nio.ByteBuffer)53 SelectionKey (java.nio.channels.SelectionKey)41 ServerSocket (java.net.ServerSocket)37 Test (org.junit.Test)27 Socket (java.net.Socket)24 ClosedChannelException (java.nio.channels.ClosedChannelException)22 Selector (java.nio.channels.Selector)22 ClosedSelectorException (java.nio.channels.ClosedSelectorException)15 InetAddress (java.net.InetAddress)14 CancelledKeyException (java.nio.channels.CancelledKeyException)11 EOFException (java.io.EOFException)8 SocketException (java.net.SocketException)8 BindException (java.net.BindException)7 SocketAddress (java.net.SocketAddress)7 Test (org.junit.jupiter.api.Test)7 OutgoingPacket (com.twitter.heron.common.network.OutgoingPacket)6