Search in sources :

Example 81 with ServerSocket

use of java.net.ServerSocket in project robovm by robovm.

the class ServerSocketChannelTest method testAccept_Block_NoConnect.

public void testAccept_Block_NoConnect() throws IOException {
    assertTrue(this.serverChannel.isBlocking());
    ServerSocket gotSocket = this.serverChannel.socket();
    gotSocket.bind(localAddr1);
    // blocking mode , will block and wait for ever...
    // so must close the server channel with another thread.
    new Thread() {

        public void run() {
            try {
                Thread.sleep(TIME_UNIT);
                ServerSocketChannelTest.this.serverChannel.close();
            } catch (Exception e) {
                fail("Fail to close the server channel because of" + e.getClass().getName());
            }
        }
    }.start();
    try {
        this.serverChannel.accept();
        fail("Should throw a AsynchronousCloseException");
    } catch (AsynchronousCloseException e) {
    // OK.
    }
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ServerSocket(java.net.ServerSocket) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) NotYetBoundException(java.nio.channels.NotYetBoundException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Example 82 with ServerSocket

use of java.net.ServerSocket in project robovm by robovm.

the class ServerSocketChannelTest method testAccept_NonBlock_NoConnect.

public void testAccept_NonBlock_NoConnect() throws IOException {
    ServerSocket gotSocket = this.serverChannel.socket();
    gotSocket.bind(localAddr1);
    this.serverChannel.configureBlocking(false);
    // non-blocking mode , will immediately return
    assertNull(this.serverChannel.accept());
}
Also used : ServerSocket(java.net.ServerSocket)

Example 83 with ServerSocket

use of java.net.ServerSocket in project robovm by robovm.

the class ServerSocketChannelTest method testSocket_Block_BeforeClose.

// -------------------------------------------------------------------
// Test for socket()
// -------------------------------------------------------------------
/*
     * Test method for 'java.nio.channels.ServerSocketChannel.socket()'
     */
public void testSocket_Block_BeforeClose() throws Exception {
    assertTrue(this.serverChannel.isOpen());
    assertTrue(this.serverChannel.isBlocking());
    ServerSocket s1 = this.serverChannel.socket();
    assertFalse(s1.isClosed());
    assertSocketNotAccepted(s1);
    ServerSocket s2 = this.serverChannel.socket();
    // same
    assertSame(s1, s2);
    // socket close makes the channel close
    s1.close();
    assertFalse(this.serverChannel.isOpen());
}
Also used : ServerSocket(java.net.ServerSocket)

Example 84 with ServerSocket

use of java.net.ServerSocket in project robovm by robovm.

the class SelectionKeyTest method test_readyOps.

/**
     * @tests java.nio.channels.SelectionKey#readyOps()
     */
public void test_readyOps() throws IOException {
    int port = Support_PortManager.getNextPort();
    ServerSocket ss = new ServerSocket(port);
    try {
        sc.connect(new InetSocketAddress(LOCAL_ADDR, port));
        assertEquals(0, selectionKey.readyOps());
        assertFalse(selectionKey.isConnectable());
        selector.select();
        assertEquals(SelectionKey.OP_CONNECT, selectionKey.readyOps());
    } finally {
        ss.close();
        ss = null;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket)

Example 85 with ServerSocket

use of java.net.ServerSocket in project robovm by robovm.

the class SocketChannelTest method test_finishConnect.

/**
     * Regression test for Harmony-1947.
     */
public void test_finishConnect() throws Exception {
    SocketAddress address = new InetSocketAddress("localhost", 0);
    ServerSocketChannel theServerChannel = ServerSocketChannel.open();
    ServerSocket serversocket = theServerChannel.socket();
    serversocket.setReuseAddress(true);
    // Bind the socket
    serversocket.bind(address);
    boolean doneNonBlockingConnect = false;
    // Loop so that we make sure we're definitely testing finishConnect()
    while (!doneNonBlockingConnect) {
        channel1 = SocketChannel.open();
        // Set the SocketChannel to non-blocking so that connect(..) does
        // not block
        channel1.configureBlocking(false);
        boolean connected = channel1.connect(new InetSocketAddress("localhost", serversocket.getLocalPort()));
        if (!connected) {
            // Now set the SocketChannel back to blocking so that
            // finishConnect() blocks.
            channel1.configureBlocking(true);
            doneNonBlockingConnect = channel1.finishConnect();
        }
        if (doneNonBlockingConnect) {
            tryFinish();
        }
        channel1.close();
    }
    if (!serversocket.isClosed()) {
        serversocket.close();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Aggregations

ServerSocket (java.net.ServerSocket)736 IOException (java.io.IOException)336 Socket (java.net.Socket)265 InetSocketAddress (java.net.InetSocketAddress)131 Test (org.junit.Test)118 SocketException (java.net.SocketException)56 InputStream (java.io.InputStream)51 SocketTimeoutException (java.net.SocketTimeoutException)43 OutputStream (java.io.OutputStream)41 InetAddress (java.net.InetAddress)41 BindException (java.net.BindException)28 URL (java.net.URL)28 SSLServerSocket (javax.net.ssl.SSLServerSocket)26 InputStreamReader (java.io.InputStreamReader)24 UnknownHostException (java.net.UnknownHostException)24 File (java.io.File)23 BufferedReader (java.io.BufferedReader)21 SSLSocket (javax.net.ssl.SSLSocket)21 DatagramSocket (java.net.DatagramSocket)20 ServerSocketChannel (java.nio.channels.ServerSocketChannel)16