Search in sources :

Example 61 with ServerSocket

use of java.net.ServerSocket in project flink by apache.

the class SocketTextStreamFunctionTest method testSocketSourceOutputAcrossRetries.

@Test
public void testSocketSourceOutputAcrossRetries() throws Exception {
    ServerSocket server = new ServerSocket(0);
    Socket channel = null;
    try {
        SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 10, 100);
        SocketSourceThread runner = new SocketSourceThread(source, "test1", "check1", "check2");
        runner.start();
        // first connection: nothing
        channel = server.accept();
        channel.close();
        // second connection: first string
        channel = server.accept();
        OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream());
        writer.write("te");
        writer.close();
        channel.close();
        // third connection: nothing
        channel = server.accept();
        channel.close();
        // forth connection: second string
        channel = server.accept();
        writer = new OutputStreamWriter(channel.getOutputStream());
        writer.write("st1\n");
        writer.write("check1\n");
        writer.write("check2\n");
        writer.flush();
        runner.waitForNumElements(2);
        runner.cancel();
        runner.waitUntilDone();
    } finally {
        if (channel != null) {
            IOUtils.closeQuietly(channel);
        }
        IOUtils.closeQuietly(server);
    }
}
Also used : ServerSocket(java.net.ServerSocket) OutputStreamWriter(java.io.OutputStreamWriter) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 62 with ServerSocket

use of java.net.ServerSocket in project flink by apache.

the class SocketTextStreamFunctionTest method testSocketSourceSimpleOutput.

@Test
public void testSocketSourceSimpleOutput() throws Exception {
    ServerSocket server = new ServerSocket(0);
    Socket channel = null;
    try {
        SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 0);
        SocketSourceThread runner = new SocketSourceThread(source, "test1", "check");
        runner.start();
        channel = server.accept();
        OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream());
        writer.write("test1\n");
        writer.write("check\n");
        writer.flush();
        runner.waitForNumElements(2);
        runner.cancel();
        runner.interrupt();
        runner.waitUntilDone();
        channel.close();
    } finally {
        if (channel != null) {
            IOUtils.closeQuietly(channel);
        }
        IOUtils.closeQuietly(server);
    }
}
Also used : ServerSocket(java.net.ServerSocket) OutputStreamWriter(java.io.OutputStreamWriter) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 63 with ServerSocket

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

the class FtpURLConnection method connectInternal.

private void connectInternal() throws IOException {
    int port = url.getPort();
    int connectTimeout = getConnectTimeout();
    if (port <= 0) {
        port = FTP_PORT;
    }
    if (currentProxy == null || Proxy.Type.HTTP == currentProxy.type()) {
        controlSocket = new Socket();
    } else {
        controlSocket = new Socket(currentProxy);
    }
    InetSocketAddress addr = new InetSocketAddress(hostName, port);
    controlSocket.connect(addr, connectTimeout);
    connected = true;
    ctrlOutput = controlSocket.getOutputStream();
    ctrlInput = controlSocket.getInputStream();
    login();
    setType();
    if (!getDoInput()) {
        cd();
    }
    try {
        acceptSocket = new ServerSocket(0);
        dataPort = acceptSocket.getLocalPort();
        /* Cannot set REUSEADDR so we need to send a PORT command */
        port();
        if (connectTimeout == 0) {
            // set timeout rather than zero as before
            connectTimeout = 3000;
        }
        acceptSocket.setSoTimeout(getConnectTimeout());
        if (getDoInput()) {
            getFile();
        } else {
            sendFile();
        }
        dataSocket = acceptSocket.accept();
        dataSocket.setSoTimeout(getReadTimeout());
        acceptSocket.close();
    } catch (InterruptedIOException e) {
        throw new IOException("Could not establish data connection");
    }
    if (getDoInput()) {
        inputStream = new FtpURLInputStream(new BufferedInputStream(dataSocket.getInputStream()), controlSocket);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) BufferedInputStream(java.io.BufferedInputStream) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 64 with ServerSocket

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

the class NativeCryptoTest method test_SSL_do_handshake_client_timeout.

public void test_SSL_do_handshake_client_timeout() throws Exception {
    // client timeout
    final ServerSocket listener = new ServerSocket(0);
    Socket serverSocket = null;
    try {
        Hooks cHooks = new Hooks();
        Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates());
        Future<TestSSLHandshakeCallbacks> client = handshake(listener, 1, true, cHooks, null, null);
        Future<TestSSLHandshakeCallbacks> server = handshake(listener, -1, false, sHooks, null, null);
        serverSocket = server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS).getSocket();
        client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        fail();
    } catch (ExecutionException expected) {
        if (SocketTimeoutException.class != expected.getCause().getClass()) {
            expected.printStackTrace();
        }
        assertEquals(SocketTimeoutException.class, expected.getCause().getClass());
    } finally {
        // Manually close peer socket when testing timeout
        IoUtils.closeQuietly(serverSocket);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ServerSocket(java.net.ServerSocket) ExecutionException(java.util.concurrent.ExecutionException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 65 with ServerSocket

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

the class NativeCryptoTest method handshake.

public static Future<TestSSLHandshakeCallbacks> handshake(final ServerSocket listener, final int timeout, final boolean client, final Hooks hooks, final byte[] npnProtocols, final byte[] alpnProtocols) {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<TestSSLHandshakeCallbacks> future = executor.submit(new Callable<TestSSLHandshakeCallbacks>() {

        @Override
        public TestSSLHandshakeCallbacks call() throws Exception {
            Socket socket = (client ? new Socket(listener.getInetAddress(), listener.getLocalPort()) : listener.accept());
            if (timeout == -1) {
                return new TestSSLHandshakeCallbacks(socket, 0, null);
            }
            FileDescriptor fd = socket.getFileDescriptor$();
            long c = hooks.getContext();
            long s = hooks.beforeHandshake(c);
            TestSSLHandshakeCallbacks callback = new TestSSLHandshakeCallbacks(socket, s, hooks);
            if (DEBUG) {
                System.out.println("ssl=0x" + Long.toString(s, 16) + " handshake" + " context=0x" + Long.toString(c, 16) + " socket=" + socket + " fd=" + fd + " timeout=" + timeout + " client=" + client);
            }
            long session = NULL;
            try {
                session = NativeCrypto.SSL_do_handshake(s, fd, callback, timeout, client, npnProtocols, alpnProtocols);
                if (DEBUG) {
                    System.out.println("ssl=0x" + Long.toString(s, 16) + " handshake" + " session=0x" + Long.toString(session, 16));
                }
            } finally {
                // Ensure afterHandshake is called to free resources
                hooks.afterHandshake(session, s, c, socket, fd, callback);
            }
            return callback;
        }
    });
    executor.shutdown();
    return future;
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) SocketTimeoutException(java.net.SocketTimeoutException) SSLProtocolException(javax.net.ssl.SSLProtocolException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) SSLException(javax.net.ssl.SSLException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor)

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