Java Code Examples for java.net.ServerSocket#setSoTimeout()

The following examples show how to use java.net.ServerSocket#setSoTimeout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PythonPlanStreamer.java    From Flink-CEPplus with Apache License 2.0 7 votes vote down vote up
private void startPython(String tmpPath, String args) throws IOException {
	String pythonBinaryPath = config.getString(PythonOptions.PYTHON_BINARY_PATH);

	try {
		Runtime.getRuntime().exec(pythonBinaryPath);
	} catch (IOException ignored) {
		throw new RuntimeException(pythonBinaryPath + " does not point to a valid python binary.");
	}
	process = Runtime.getRuntime().exec(pythonBinaryPath + " -B " + tmpPath + FLINK_PYTHON_PLAN_NAME + args);

	new Thread(new StreamPrinter(process.getInputStream())).start();
	new Thread(new StreamPrinter(process.getErrorStream())).start();

	server = new ServerSocket(0);
	server.setSoTimeout(50);

	process.getOutputStream().write("plan\n".getBytes(ConfigConstants.DEFAULT_CHARSET));
	process.getOutputStream().flush();
}
 
Example 2
Source File: ServerThread.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean createServerSocket(DebugSession debugSession) {
    synchronized (ServerThread.class) {
        try {
            myPort = debugSession.getOptions().getPort();
            myServer = new ServerSocket(myPort);
            myServer.setSoTimeout(TIMEOUT);
            myServer.setReuseAddress(true);
        } catch (IOException e) {
            String mesg = NbBundle.getMessage(ServerThread.class, PORT_OCCUPIED);
            mesg = MessageFormat.format(mesg, myPort);
            NotifyDescriptor descriptor = new NotifyDescriptor.Confirmation(mesg, JOptionPane.YES_NO_OPTION);
            Object choice = DialogDisplayer.getDefault().notify(descriptor);
            if (choice.equals(JOptionPane.YES_OPTION)) {
                Utils.openPhpOptionsDialog();
            }
            log(e);
            return false;
        }
        return true;
    }
}
 
Example 3
Source File: StreamingRetry.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
void test() throws IOException {
    ss = new ServerSocket(0);
    ss.setSoTimeout(ACCEPT_TIMEOUT);
    int port = ss.getLocalPort();

    (new Thread(this)).start();

    try {
        URL url = new URL("http://localhost:" + port + "/");
        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setDoOutput(true);
        uc.setChunkedStreamingMode(4096);
        OutputStream os = uc.getOutputStream();
        os.write("Hello there".getBytes());

        InputStream is = uc.getInputStream();
        is.close();
    } catch (IOException expected) {
        //expected.printStackTrace();
    } finally {
        ss.close();
    }
}
 
Example 4
Source File: StreamingRetry.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void test(String method) throws Exception {
    ss = new ServerSocket(0);
    ss.setSoTimeout(ACCEPT_TIMEOUT);
    int port = ss.getLocalPort();

    Thread otherThread = new Thread(this);
    otherThread.start();

    try {
        URL url = new URL("http://localhost:" + port + "/");
        HttpURLConnection uc = (HttpURLConnection) url.openConnection();
        uc.setDoOutput(true);
        if (method != null)
            uc.setRequestMethod(method);
        uc.setChunkedStreamingMode(4096);
        OutputStream os = uc.getOutputStream();
        os.write("Hello there".getBytes());

        InputStream is = uc.getInputStream();
        is.close();
    } catch (IOException expected) {
        //expected.printStackTrace();
    } finally {
        ss.close();
        otherThread.join();
    }
}
 
Example 5
Source File: SocketProperties.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void setProperties(ServerSocket socket) throws SocketException{
    if (rxBufSize != null)
        socket.setReceiveBufferSize(rxBufSize.intValue());
    if (performanceConnectionTime != null && performanceLatency != null &&
            performanceBandwidth != null)
        socket.setPerformancePreferences(
                performanceConnectionTime.intValue(),
                performanceLatency.intValue(),
                performanceBandwidth.intValue());
    if (soReuseAddress != null)
        socket.setReuseAddress(soReuseAddress.booleanValue());
    if (soTimeout != null && soTimeout.intValue() >= 0)
        socket.setSoTimeout(soTimeout.intValue());
}
 
Example 6
Source File: SelectFdsLimit.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String [] args) throws IOException, FileNotFoundException {

        //The bug 8021820 is a Mac specific and because of that test will pass on all
        //other platforms
        if (!System.getProperty("os.name").contains("OS X")) {
           return;
        }

        //Create test directory with test files
        prepareTestEnv();

        //Consume FD ids for this java process to overflow the 1024
        openFiles(FDTOOPEN,new File(TESTFILE));

        //Wait for incoming connection and make the select() used in java.net
        //classes fail the limitation on FDSET_SIZE
        ServerSocket socket = new ServerSocket(0);

        //Set the minimal timeout, no one is
        //going to connect to this server socket
        socket.setSoTimeout(1);

        // The accept() call will throw SocketException if the
        // select() has failed due to limitation on fds size,
        // indicating test failure. A SocketTimeoutException
        // is expected, so it is caught and ignored, and the test
        // passes.
        try {
           socket.accept();
        } catch (SocketTimeoutException e) { }
    }
 
Example 7
Source File: ConnectionManager.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
   Listen for connections on a particular port.
   @param port The port to listen on.
   @param registry The registry to install in new connections.
   @param listener A ConnectionManagerListener that will be informed of new connections.
   @throws IOException If there is a problem listening on the port.
*/
public void listen(int port, Registry registry, ConnectionManagerListener listener) throws IOException {
    synchronized (lock) {
        if (shutdown) {
            throw new IOException("Connection manager has been shut down");
        }
        Logger.info("Listening for connections on port " + port);
        ServerSocket socket = new ServerSocket(port);
        socket.setSoTimeout(1000);
        socket.setReuseAddress(true);
        Reader r = new Reader(socket, registry, listener);
        readers.add(r);
        r.start();
    }
}
 
Example 8
Source File: SelectFdsLimit.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String [] args) throws IOException, FileNotFoundException {

        //The bug 8021820 is a Mac specific and because of that test will pass on all
        //other platforms
        if (!System.getProperty("os.name").contains("OS X")) {
           return;
        }

        //Create test directory with test files
        prepareTestEnv();

        //Consume FD ids for this java process to overflow the 1024
        openFiles(FDTOOPEN,new File(TESTFILE));

        //Wait for incoming connection and make the select() used in java.net
        //classes fail the limitation on FDSET_SIZE
        ServerSocket socket = new ServerSocket(0);

        //Set the minimal timeout, no one is
        //going to connect to this server socket
        socket.setSoTimeout(1);

        // The accept() call will throw SocketException if the
        // select() has failed due to limitation on fds size,
        // indicating test failure. A SocketTimeoutException
        // is expected, so it is caught and ignored, and the test
        // passes.
        try {
           socket.accept();
        } catch (SocketTimeoutException e) { }
    }
 
Example 9
Source File: JSSESocketFactory.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the certificate is compatible with the enabled cipher suites.
 * If we don't check now, the JIoEndpoint can enter a nasty logging loop.
 * See bug 45528.
 */
private void checkConfig() throws IOException {
    // Create an unbound server socket
    ServerSocket socket = sslProxy.createServerSocket();
    initServerSocket(socket);

    try {
        // Set the timeout to 1ms as all we care about is if it throws an
        // SSLException on accept.
        socket.setSoTimeout(1);

        socket.accept();
        // Will never get here - no client can connect to an unbound port
    } catch (SSLException ssle) {
        // SSL configuration is invalid. Possibly cert doesn't match ciphers
        IOException ioe = new IOException(sm.getString(
                "jsse.invalid_ssl_conf", ssle.getMessage()));
        ioe.initCause(ssle);
        throw ioe;
    } catch (Exception e) {
        /*
         * Possible ways of getting here
         * socket.accept() throws a SecurityException
         * socket.setSoTimeout() throws a SocketException
         * socket.accept() throws some other exception (after a JDK change)
         *      In these cases the test won't work so carry on - essentially
         *      the behaviour before this patch
         * socket.accept() throws a SocketTimeoutException
         *      In this case all is well so carry on
         */
    } finally {
        // Should be open here but just in case
        if (!socket.isClosed()) {
            socket.close();
        }
    }

}
 
Example 10
Source File: SelectFdsLimit.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String [] args) throws IOException, FileNotFoundException {

        //The bug 8021820 is a Mac specific and because of that test will pass on all
        //other platforms
        if (!System.getProperty("os.name").contains("OS X")) {
           return;
        }

        //Create test directory with test files
        prepareTestEnv();

        //Consume FD ids for this java process to overflow the 1024
        openFiles(FDTOOPEN,new File(TESTFILE));

        //Wait for incoming connection and make the select() used in java.net
        //classes fail the limitation on FDSET_SIZE
        ServerSocket socket = new ServerSocket(0);

        //Set the minimal timeout, no one is
        //going to connect to this server socket
        socket.setSoTimeout(1);

        // The accept() call will throw SocketException if the
        // select() has failed due to limitation on fds size,
        // indicating test failure. A SocketTimeoutException
        // is expected, so it is caught and ignored, and the test
        // passes.
        try {
           socket.accept();
        } catch (SocketTimeoutException e) { }
    }
 
Example 11
Source File: SelectFdsLimit.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String [] args) throws IOException, FileNotFoundException {

        //The bug 8021820 is a Mac specific and because of that test will pass on all
        //other platforms
        if (!System.getProperty("os.name").contains("OS X")) {
           return;
        }

        //Create test directory with test files
        prepareTestEnv();

        //Consume FD ids for this java process to overflow the 1024
        openFiles(FDTOOPEN,new File(TESTFILE));

        //Wait for incoming connection and make the select() used in java.net
        //classes fail the limitation on FDSET_SIZE
        ServerSocket socket = new ServerSocket(0);

        //Set the minimal timeout, no one is
        //going to connect to this server socket
        socket.setSoTimeout(1);

        // The accept() call will throw SocketException if the
        // select() has failed due to limitation on fds size,
        // indicating test failure. A SocketTimeoutException
        // is expected, so it is caught and ignored, and the test
        // passes.
        try {
           socket.accept();
        } catch (SocketTimeoutException e) { }
    }
 
Example 12
Source File: AbstractClientServerSupport.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected static ServerSocket openServerSocket(final InetAddress address, final int port, final boolean reuseAddress, final int socketTimeout)
  throws IOException
{
  final ServerSocket serverSocket = new ServerSocket();
  serverSocket.setReuseAddress(reuseAddress);
  serverSocket.setSoTimeout(socketTimeout);
  serverSocket.bind(new InetSocketAddress(address, port));
  return serverSocket;
}
 
Example 13
Source File: SelectFdsLimit.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String [] args) throws IOException, FileNotFoundException {

        //The bug 8021820 is a Mac specific and because of that test will pass on all
        //other platforms
        if (!System.getProperty("os.name").contains("OS X")) {
           return;
        }

        //Create test directory with test files
        prepareTestEnv();

        //Consume FD ids for this java process to overflow the 1024
        openFiles(FDTOOPEN,new File(TESTFILE));

        //Wait for incoming connection and make the select() used in java.net
        //classes fail the limitation on FDSET_SIZE
        ServerSocket socket = new ServerSocket(0);

        //Set the minimal timeout, no one is
        //going to connect to this server socket
        socket.setSoTimeout(1);

        // The accept() call will throw SocketException if the
        // select() has failed due to limitation on fds size,
        // indicating test failure. A SocketTimeoutException
        // is expected, so it is caught and ignored, and the test
        // passes.
        try {
           socket.accept();
        } catch (SocketTimeoutException e) { }
    }
 
Example 14
Source File: SelectFdsLimit.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String [] args) throws IOException, FileNotFoundException {

        //The bug 8021820 is a Mac specific and because of that test will pass on all
        //other platforms
        if (!System.getProperty("os.name").contains("OS X")) {
           return;
        }

        //Create test directory with test files
        prepareTestEnv();

        //Consume FD ids for this java process to overflow the 1024
        openFiles(FDTOOPEN,new File(TESTFILE));

        //Wait for incoming connection and make the select() used in java.net
        //classes fail the limitation on FDSET_SIZE
        ServerSocket socket = new ServerSocket(0);

        //Set the minimal timeout, no one is
        //going to connect to this server socket
        socket.setSoTimeout(1);

        // The accept() call will throw SocketException if the
        // select() has failed due to limitation on fds size,
        // indicating test failure. A SocketTimeoutException
        // is expected, so it is caught and ignored, and the test
        // passes.
        try {
           socket.accept();
        } catch (SocketTimeoutException e) { }
    }
 
Example 15
Source File: ServerSocketConnectionFactory.java    From jsmpp with Apache License 2.0 4 votes vote down vote up
public ServerConnection listen(int port, int timeout) throws IOException {
    ServerSocket ss = new ServerSocket(port);
    ss.setSoTimeout(timeout);
    return new ServerSocketConnection(ss);
}
 
Example 16
Source File: SSLServerSocketConnectionFactory.java    From jsmpp with Apache License 2.0 4 votes vote down vote up
public ServerConnection listen(int port, int timeout) throws IOException {
  ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();
  ServerSocket serverSocket = serverSocketFactory.createServerSocket(port);
  serverSocket.setSoTimeout(timeout);
  return new ServerSocketConnection(serverSocket);
}
 
Example 17
Source File: FlowableServerSocket.java    From rxjava2-extras with Apache License 2.0 4 votes vote down vote up
private static ServerSocket createServerSocket(Callable<? extends ServerSocket> serverSocketCreator, long timeoutMs)
        throws Exception {
    ServerSocket s = serverSocketCreator.call();
    s.setSoTimeout((int) timeoutMs);
    return s;
}
 
Example 18
Source File: ObservableServerSocket.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
private static ServerSocket createServerSocket(
        Func0<? extends ServerSocket> serverSocketCreator, long timeoutMs) throws IOException {
    ServerSocket s = serverSocketCreator.call();
    s.setSoTimeout((int) timeoutMs);
    return s;
}
 
Example 19
Source File: KeyStoreSSLServerSocketConnectionFactory.java    From jsmpp with Apache License 2.0 4 votes vote down vote up
public ServerConnection listen(int port, int timeout) throws IOException {
  ServerSocket serverSocket = sslServerSocketFactory.createServerSocket(port);
  serverSocket.setSoTimeout(timeout);
  return new ServerSocketConnection(serverSocket);
}
 
Example 20
Source File: JMeterDaemon.java    From jsflight with Apache License 2.0 3 votes vote down vote up
/**
 * Create a new Daemon with the specified port and target, using the
 * specified class to handle individual requests.
 *
 * @param port       the port to listen on.
 * @param target     the target which will receive the generated JMeter test
 *                   components.
 * @param proxyClass the proxy class to use to handle individual requests. This
 *                   class must be the {@link Proxy} class or a subclass.
 * @throws IOException              if an I/O error occurs opening the socket
 * @throws IllegalArgumentException if <code>port</code> is outside the allowed range from <code>0</code> to <code>65535</code>
 * @throws SocketException          when something is wrong on the underlying protocol layer
 */
public JMeterDaemon(int port, JMeterProxyControl target, Class<? extends JMeterProxy> proxyClass)
        throws IOException
{
    super("HTTP Proxy Daemon");
    this.target = target;
    this.daemonPort = port;
    this.proxyClass = proxyClass;
    LOG.info("Creating Daemon Socket on port: " + daemonPort);
    mainSocket = new ServerSocket(daemonPort);
    mainSocket.setSoTimeout(ACCEPT_TIMEOUT);
}