Search in sources :

Example 86 with ServerSocket

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

the class SocketChannelTest method assertSocketAction_Block_BeforeConnect.

private void assertSocketAction_Block_BeforeConnect(Socket s) throws IOException {
    assertFalse(this.channel1.isConnected());
    this.server2 = new ServerSocket(localAddr2.getPort());
    s.connect(localAddr2);
    assertTrue(this.channel1.isConnected());
    assertTrue(s.isConnected());
    assertSocketAfterConnect(s, localAddr2);
    try {
        s.bind(localAddr2);
        fail("Should throw AlreadyConnectedException");
    } catch (AlreadyConnectedException e) {
    // OK.
    }
    s.close();
    assertTrue(s.isClosed());
    assertFalse(this.channel1.isOpen());
}
Also used : AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) ServerSocket(java.net.ServerSocket)

Example 87 with ServerSocket

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

the class SocketChannelTest method ensureServerOpen.

private void ensureServerOpen() throws IOException {
    ensureServerClosed();
    this.server1 = new ServerSocket(localAddr1.getPort());
    this.server2 = new ServerSocket(localAddr2.getPort());
    assertTrue(this.server1.isBound());
    assertTrue(this.server2.isBound());
}
Also used : ServerSocket(java.net.ServerSocket)

Example 88 with ServerSocket

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

the class SocketChannelTest method testReadByteBufferArray.

/*
     * Test method for 'java.nio.channels.SocketChannel.read(ByteBuffer[])'
     */
public void testReadByteBufferArray() throws IOException {
    java.nio.ByteBuffer[] byteBuf = null;
    MockSocketChannel testMSChannelnull = new MockSocketChannel(null);
    MockSocketChannel testMSChannel = new MockSocketChannel(SelectorProvider.provider());
    ServerSocket testServer = new ServerSocket(Support_PortManager.getNextPort());
    try {
        try {
            this.channel1.read(byteBuf);
            fail("Should throw NPE");
        } catch (NullPointerException e) {
        // correct
        }
        byteBuf = new java.nio.ByteBuffer[CAPACITY_NORMAL];
        try {
            this.channel1.read(byteBuf);
            fail("Should throw NotYetConnectedException");
        } catch (NotYetConnectedException e) {
        // correct
        }
        long readNum = CAPACITY_NORMAL;
        readNum = testMSChannel.read(byteBuf);
        assertEquals(0, readNum);
        readNum = CAPACITY_NORMAL;
        readNum = testMSChannelnull.read(byteBuf);
        assertEquals(0, readNum);
    } finally {
        testServer.close();
    }
}
Also used : ServerSocket(java.net.ServerSocket) NotYetConnectedException(java.nio.channels.NotYetConnectedException) ByteBuffer(java.nio.ByteBuffer)

Example 89 with ServerSocket

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

the class SocketChannelTest method test_read_intoReadOnlyByteArrays.

public void test_read_intoReadOnlyByteArrays() throws Exception {
    ByteBuffer readOnly = ByteBuffer.allocate(1).asReadOnlyBuffer();
    ServerSocket ss = new ServerSocket(0);
    ss.setReuseAddress(true);
    SocketChannel sc = SocketChannel.open(ss.getLocalSocketAddress());
    try {
        sc.read(readOnly);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        sc.read(new ByteBuffer[] { readOnly });
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        sc.read(new ByteBuffer[] { readOnly }, 0, 1);
        fail();
    } catch (IllegalArgumentException expected) {
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocket(java.net.ServerSocket) ByteBuffer(java.nio.ByteBuffer)

Example 90 with ServerSocket

use of java.net.ServerSocket in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testClientStopsServerDoesNotCloseClientCloses.

@Test
public void testClientStopsServerDoesNotCloseClientCloses() throws Exception {
    try (ServerSocket server = new ServerSocket(0)) {
        List<Session> sessions = new ArrayList<>();
        HTTP2Client h2Client = new HTTP2Client();
        HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(h2Client) {

            @Override
            protected HttpConnectionOverHTTP2 newHttpConnection(HttpDestination destination, Session session) {
                sessions.add(session);
                return super.newHttpConnection(destination, session);
            }
        }, null);
        QueuedThreadPool clientExecutor = new QueuedThreadPool();
        clientExecutor.setName("client");
        client.setExecutor(clientExecutor);
        client.start();
        CountDownLatch resultLatch = new CountDownLatch(1);
        client.newRequest("localhost", server.getLocalPort()).send(result -> {
            if (result.getResponse().getStatus() == HttpStatus.OK_200)
                resultLatch.countDown();
        });
        ByteBufferPool byteBufferPool = new MappedByteBufferPool();
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        Generator generator = new Generator(byteBufferPool);
        try (Socket socket = server.accept()) {
            socket.setSoTimeout(1000);
            OutputStream output = socket.getOutputStream();
            InputStream input = socket.getInputStream();
            ServerParser parser = new ServerParser(byteBufferPool, new ServerParser.Listener.Adapter() {

                @Override
                public void onHeaders(HeadersFrame request) {
                    // Server's preface.
                    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
                    // Reply to client's SETTINGS.
                    generator.control(lease, new SettingsFrame(new HashMap<>(), true));
                    // Response.
                    MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    HeadersFrame response = new HeadersFrame(request.getStreamId(), metaData, null, true);
                    generator.control(lease, response);
                    try {
                        // Write the frames.
                        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
                    } catch (Throwable x) {
                        x.printStackTrace();
                    }
                }
            }, 4096, 8192);
            byte[] bytes = new byte[1024];
            while (true) {
                try {
                    int read = input.read(bytes);
                    if (read < 0)
                        Assert.fail();
                    parser.parse(ByteBuffer.wrap(bytes, 0, read));
                } catch (SocketTimeoutException x) {
                    break;
                }
            }
            Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
            // The client will send a GO_AWAY, but the server will not close.
            client.stop();
            // Give some time to process the stop/close operations.
            Thread.sleep(1000);
            Assert.assertTrue(h2Client.getBeans(Session.class).isEmpty());
            for (Session session : sessions) {
                Assert.assertTrue(session.isClosed());
                Assert.assertTrue(((HTTP2Session) session).isDisconnected());
            }
        }
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) SocketTimeoutException(java.net.SocketTimeoutException) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) HttpDestination(org.eclipse.jetty.client.HttpDestination) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

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