Search in sources :

Example 51 with ServerSocketChannel

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

the class AbstractSelectorTest method test_register_LSelectorI.

/**
     * @tests AbstractSelector#register(Selector,int)
     */
public void test_register_LSelectorI() throws Exception {
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.configureBlocking(false);
    assertFalse(ssc.isRegistered());
    SelectionKey acceptKey = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
    assertTrue(ssc.isRegistered());
    assertNotNull(acceptKey);
    assertTrue(acceptSelector.keys().contains(acceptKey));
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 52 with ServerSocketChannel

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

the class SocketTest method checkSocketLocalAndRemoteAddresses.

public void checkSocketLocalAndRemoteAddresses(boolean setOptions) throws Exception {
    InetAddress host = InetAddress.getLocalHost();
    // Open a local server port.
    ServerSocketChannel ssc = ServerSocketChannel.open();
    InetSocketAddress listenAddr = new InetSocketAddress(host, 0);
    ssc.socket().bind(listenAddr, 0);
    ServerSocket ss = ssc.socket();
    // Open a socket to the local port.
    SocketChannel out = SocketChannel.open();
    out.configureBlocking(false);
    if (setOptions) {
        out.socket().setTcpNoDelay(false);
    }
    InetSocketAddress addr = new InetSocketAddress(host, ssc.socket().getLocalPort());
    out.connect(addr);
    while (!out.finishConnect()) {
        Thread.sleep(1);
    }
    SocketChannel in = ssc.accept();
    if (setOptions) {
        in.socket().setTcpNoDelay(false);
    }
    InetSocketAddress outRemoteAddress = (InetSocketAddress) out.socket().getRemoteSocketAddress();
    InetSocketAddress outLocalAddress = (InetSocketAddress) out.socket().getLocalSocketAddress();
    InetSocketAddress inLocalAddress = (InetSocketAddress) in.socket().getLocalSocketAddress();
    InetSocketAddress inRemoteAddress = (InetSocketAddress) in.socket().getRemoteSocketAddress();
    System.err.println("inLocalAddress: " + inLocalAddress);
    System.err.println("inRemoteAddress: " + inRemoteAddress);
    System.err.println("outLocalAddress: " + outLocalAddress);
    System.err.println("outRemoteAddress: " + outRemoteAddress);
    assertEquals(outRemoteAddress.getPort(), ss.getLocalPort());
    assertEquals(inLocalAddress.getPort(), ss.getLocalPort());
    assertEquals(inRemoteAddress.getPort(), outLocalAddress.getPort());
    assertEquals(inLocalAddress.getAddress(), ss.getInetAddress());
    assertEquals(inRemoteAddress.getAddress(), ss.getInetAddress());
    assertEquals(outLocalAddress.getAddress(), ss.getInetAddress());
    assertEquals(outRemoteAddress.getAddress(), ss.getInetAddress());
    in.close();
    out.close();
    ssc.close();
    assertNull(in.socket().getRemoteSocketAddress());
    assertNull(out.socket().getRemoteSocketAddress());
    assertEquals(in.socket().getLocalSocketAddress(), ss.getLocalSocketAddress());
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 53 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project chuidiang-ejemplos by chuidiang.

the class Client method main.

public static void main(String[] args) {
    try (ServerSocketChannel server = ServerSocketChannel.open()) {
        server.bind(new InetSocketAddress("0.0.0.0", 5557));
        while (true) {
            SocketChannel client = server.accept();
            new Thread(new Client(client)).start();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 54 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project chuidiang-ejemplos by chuidiang.

the class Client method main.

public static void main(String[] args) throws IOException {
    ServerSocketChannel server = ServerSocketChannel.open();
    server.bind(new InetSocketAddress("127.0.0.1", 5557));
    while (true) {
        SocketChannel client = server.accept();
        new Thread(new Client(client)).start();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 55 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project jdk8u_jdk by JetBrains.

the class CloseAfterConnect method main.

public static void main(String[] args) throws Exception {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(new InetSocketAddress(0));
    InetAddress lh = InetAddress.getLocalHost();
    final SocketChannel sc = SocketChannel.open();
    final InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort());
    // establish connection in another thread
    Runnable connector = new Runnable() {

        public void run() {
            try {
                sc.connect(isa);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    };
    Thread thr = new Thread(connector);
    thr.start();
    // terminate
    do {
        try {
            thr.join();
        } catch (InterruptedException x) {
        }
    } while (thr.isAlive());
    // check connection is established
    if (!sc.isConnected()) {
        throw new RuntimeException("SocketChannel not connected");
    }
    // close channel - this triggered the bug as it attempted to signal
    // a thread that no longer exists
    sc.close();
    // clean-up
    ssc.accept().close();
    ssc.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) InetAddress(java.net.InetAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Aggregations

ServerSocketChannel (java.nio.channels.ServerSocketChannel)122 SocketChannel (java.nio.channels.SocketChannel)65 InetSocketAddress (java.net.InetSocketAddress)55 IOException (java.io.IOException)43 ByteBuffer (java.nio.ByteBuffer)26 ServerSocket (java.net.ServerSocket)19 SelectionKey (java.nio.channels.SelectionKey)19 Selector (java.nio.channels.Selector)13 Socket (java.net.Socket)12 Test (org.junit.Test)11 ClosedChannelException (java.nio.channels.ClosedChannelException)10 InetAddress (java.net.InetAddress)9 ClosedSelectorException (java.nio.channels.ClosedSelectorException)8 SocketException (java.net.SocketException)6 Benchmark (org.openjdk.jmh.annotations.Benchmark)6 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)5 URISyntaxException (java.net.URISyntaxException)4 CancelledKeyException (java.nio.channels.CancelledKeyException)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4