Search in sources :

Example 91 with ServerSocket

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

the class IOTest method testReset.

@Test
public void testReset() throws Exception {
    try (ServerSocket connector = new ServerSocket(0);
        Socket client = new Socket("127.0.0.1", connector.getLocalPort());
        Socket server = connector.accept()) {
        client.setTcpNoDelay(true);
        client.setSoLinger(true, 0);
        server.setTcpNoDelay(true);
        server.setSoLinger(true, 0);
        client.getOutputStream().write(1);
        assertEquals(1, server.getInputStream().read());
        server.getOutputStream().write(1);
        assertEquals(1, client.getInputStream().read());
        // Server generator shutdowns output after non persistent sending response.
        server.shutdownOutput();
        // client endpoint reads EOF and shutdown input as result
        assertEquals(-1, client.getInputStream().read());
        client.shutdownInput();
        // client connection see's EOF and shutsdown output as no more requests to be sent.
        client.shutdownOutput();
        // Since input already shutdown, client also closes socket.
        client.close();
        // Server reads the EOF from client oshut and shut's down it's input
        assertEquals(-1, server.getInputStream().read());
        server.shutdownInput();
        // Since output was already shutdown, server closes
        server.close();
    }
}
Also used : ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 92 with ServerSocket

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

the class IOTest method testHalfClose.

@Test
public void testHalfClose() throws Exception {
    ServerSocket connector = new ServerSocket(0);
    Socket client = new Socket("localhost", connector.getLocalPort());
    Socket server = connector.accept();
    // we can write both ways
    client.getOutputStream().write(1);
    assertEquals(1, server.getInputStream().read());
    server.getOutputStream().write(1);
    assertEquals(1, client.getInputStream().read());
    // shutdown output results in read -1
    client.shutdownOutput();
    assertEquals(-1, server.getInputStream().read());
    // Even though EOF has been read, the server input is not seen as shutdown
    assertFalse(server.isInputShutdown());
    // and we can read -1 again
    assertEquals(-1, server.getInputStream().read());
    // but cannot write
    try {
        client.getOutputStream().write(1);
        fail("exception expected");
    } catch (SocketException e) {
    }
    // but can still write in opposite direction.
    server.getOutputStream().write(1);
    assertEquals(1, client.getInputStream().read());
    // server can shutdown input to match the shutdown out of client
    server.shutdownInput();
    // now we EOF instead of reading -1
    try {
        server.getInputStream().read();
        fail("exception expected");
    } catch (SocketException e) {
    }
    // but can still write in opposite direction.
    server.getOutputStream().write(1);
    assertEquals(1, client.getInputStream().read());
    // client can shutdown input
    client.shutdownInput();
    // now we EOF instead of reading -1
    try {
        client.getInputStream().read();
        fail("exception expected");
    } catch (SocketException e) {
    }
    // But we can still write at the server (data which will never be read)
    server.getOutputStream().write(1);
    // and the server output is not shutdown
    assertFalse(server.isOutputShutdown());
    // until we explictly shut it down
    server.shutdownOutput();
    // and now we can't write
    try {
        server.getOutputStream().write(1);
        fail("exception expected");
    } catch (SocketException e) {
    }
    // but the sockets are still open
    assertFalse(client.isClosed());
    assertFalse(server.isClosed());
    // but if we close one end
    client.close();
    // it is seen as closed.
    assertTrue(client.isClosed());
    // but not the other end
    assertFalse(server.isClosed());
    // which has to be closed explictly
    server.close();
    assertTrue(server.isClosed());
}
Also used : SocketException(java.net.SocketException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 93 with ServerSocket

use of java.net.ServerSocket in project elasticsearch by elastic.

the class AbstractSimpleTransportTestCase method testTcpHandshakeConnectionReset.

public void testTcpHandshakeConnectionReset() throws IOException, InterruptedException {
    try (ServerSocket socket = new MockServerSocket()) {
        socket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 1);
        socket.setReuseAddress(true);
        DiscoveryNode dummy = new DiscoveryNode("TEST", new TransportAddress(socket.getInetAddress(), socket.getLocalPort()), emptyMap(), emptySet(), version0);
        Thread t = new Thread() {

            @Override
            public void run() {
                try (Socket accept = socket.accept()) {
                    if (randomBoolean()) {
                        // sometimes wait until the other side sends the message
                        accept.getInputStream().read();
                    }
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        };
        t.start();
        ConnectionProfile.Builder builder = new ConnectionProfile.Builder();
        builder.addConnections(1, TransportRequestOptions.Type.BULK, TransportRequestOptions.Type.PING, TransportRequestOptions.Type.RECOVERY, TransportRequestOptions.Type.REG, TransportRequestOptions.Type.STATE);
        builder.setHandshakeTimeout(TimeValue.timeValueHours(1));
        ConnectTransportException ex = expectThrows(ConnectTransportException.class, () -> serviceA.connectToNode(dummy, builder.build()));
        assertEquals(ex.getMessage(), "[][" + dummy.getAddress() + "] general node connection failure");
        assertThat(ex.getCause().getMessage(), startsWith("handshake failed"));
        t.join();
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) InetSocketAddress(java.net.InetSocketAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket) ServerSocket(java.net.ServerSocket) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket)

Example 94 with ServerSocket

use of java.net.ServerSocket in project Fling by entertailion.

the class FlingFrame method vlcTranscode.

protected void vlcTranscode(final String file) {
    // Transcoding does not support jumps
    isTranscoding = true;
    // http://caprica.github.io/vlcj/javadoc/2.1.0/index.html
    try {
        // clean up previous session
        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
        if (mediaPlayerFactory != null) {
            mediaPlayerFactory.release();
        }
        mediaPlayerFactory = new MediaPlayerFactory();
        mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();
        // Add a component to be notified of player events
        mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {

            public void opening(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Opening");
            }

            public void buffering(MediaPlayer mediaPlayer, float newCache) {
                Log.d(LOG_TAG, "VLC Transcoding: Buffering");
            }

            public void playing(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Playing");
                setDuration((int) (mediaPlayer.getLength() / 1000.0f));
                Log.d(LOG_TAG, "duration=" + duration);
            }

            public void paused(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Paused");
            }

            public void stopped(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Stopped");
            }

            public void finished(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Finished");
            }

            public void error(MediaPlayer mediaPlayer) {
                Log.d(LOG_TAG, "VLC Transcoding: Error");
            }

            public void videoOutput(MediaPlayer mediaPlayer, int newCount) {
                Log.d(LOG_TAG, "VLC Transcoding: VideoOutput");
            }
        });
        // Find a port for VLC HTTP server
        boolean started = false;
        int vlcPort = port + 1;
        while (!started) {
            try {
                ServerSocket serverSocket = new ServerSocket(vlcPort);
                Log.d(LOG_TAG, "Available port for VLC: " + vlcPort);
                started = true;
                serverSocket.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
                vlcPort++;
            } catch (Exception ex) {
                break;
            }
        }
        if (!rampClient.isClosed()) {
            rampClient.stop();
        }
        Inet4Address address = getNetworAddress();
        if (address != null) {
            // Play a particular item, with options if necessary
            final String[] options = { ":sout=#transcode{" + transcodingParameterValues + "}:http{mux=webm,dst=:" + vlcPort + "/cast.webm}", ":sout-keep" };
            // http://192.168.0.8:8087/cast.webm
            final String url = "http://" + address.getHostAddress() + ":" + vlcPort + "/cast.webm";
            Log.d(LOG_TAG, "url=" + url);
            if (true || isChromeCast()) {
                rampClient.launchApp(appId == null ? APP_ID : appId, selectedDialServer);
                // wait for socket to be ready...
                new Thread(new Runnable() {

                    public void run() {
                        while (!rampClient.isStarted() && !rampClient.isClosed()) {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                            }
                        }
                        if (!rampClient.isClosed()) {
                            mediaPlayer.playMedia(file, options);
                            rampClient.load(url);
                        }
                    }
                }).start();
            } else {
                rampClient.load(url);
            }
        } else {
            Log.d(LOG_TAG, "could not find a network interface");
        }
    } catch (Throwable e) {
        Log.e(LOG_TAG, "vlcTranscode: " + file, e);
    }
}
Also used : Inet4Address(java.net.Inet4Address) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) MediaPlayerFactory(uk.co.caprica.vlcj.player.MediaPlayerFactory) MediaPlayerEventAdapter(uk.co.caprica.vlcj.player.MediaPlayerEventAdapter) MediaPlayer(uk.co.caprica.vlcj.player.MediaPlayer)

Example 95 with ServerSocket

use of java.net.ServerSocket in project android by cSploit.

the class Proxy method run.

public void run() {
    try {
        if (mSocket == null)
            mSocket = new ServerSocket(mPort, BACKLOG, mAddress);
        Log.d(TAG, "Proxy started on " + mAddress + ":" + mPort);
        mRunning = true;
        while (mRunning && mSocket != null) {
            try {
                Profiler.instance().profile("client spawn");
                Socket client = mSocket.accept();
                new ProxyThread(client, mRequestListener, mFilters, mHostRedirect, mPortRedirect).start();
                Profiler.instance().profile("client spawn");
            } catch (Exception e) {
            }
        }
        Log.d(TAG, "Proxy stopped.");
    } catch (IOException e) {
        System.errorLogging(e);
    }
}
Also used : ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) IOException(java.io.IOException)

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