Example usage for java.net ServerSocket setReuseAddress

List of usage examples for java.net ServerSocket setReuseAddress

Introduction

In this page you can find the example usage for java.net ServerSocket setReuseAddress.

Prototype

public void setReuseAddress(boolean on) throws SocketException 

Source Link

Document

Enable/disable the SocketOptions#SO_REUSEADDR SO_REUSEADDR socket option.

Usage

From source file:org.mule.transport.tcp.TcpServerSocketFactory.java

protected ServerSocket configure(ServerSocket socket, Boolean reuse, InetSocketAddress address, int backlog)
        throws IOException {
    if (null != reuse && reuse.booleanValue() != socket.getReuseAddress()) {
        socket.setReuseAddress(reuse.booleanValue());
    }/*from   w ww.j av a 2s.c o m*/
    // bind *after* setting so_reuseaddress
    socket.bind(address, backlog);
    return socket;
}

From source file:gridool.communication.transport.tcp.GridThreadPerConnectionServer.java

public void start() throws GridException {
    if (listener == null) {
        throw new IllegalStateException("GridTransportListener is not set");
    }//from  w ww  .j  a  va 2 s  .  c om

    final int port = config.getTransportServerPort();
    final ServerSocket ss;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
    } catch (IOException e) {
        String errmsg = "Could not create ServerSocket on port: " + port;
        LOG.error(errmsg, e);
        throw new GridException(errmsg);
    }

    if (LOG.isInfoEnabled()) {
        LOG.info("GridThreadPerConnectionServer is started at port: " + port);
    }

    final int readers = config.getSelectorReadThreadsCount();
    final ExecutorService execPool = ExecutorFactory.newCachedThreadPool(readers, 60L,
            "GridThreadPerConnectionServer#RequestHandler", false);
    final int msgProcs = config.getMessageProcessorPoolSize();
    final ExecutorService msgProcPool = ExecutorFactory.newFixedThreadPool(msgProcs, "OioMessageProcessor",
            false);
    this.acceptor = new AcceptorThread(ss, listener, execPool, msgProcPool);

    acceptor.start();
}

From source file:org.apache.nifi.atlas.emulator.EmbeddedKafka.java

/**
 * Will determine the available port used by Kafka/Zookeeper servers.
 *//*from ww w  .  j  a  v a 2s. co m*/
private int availablePort() {
    ServerSocket s = null;
    try {
        s = new ServerSocket(0);
        s.setReuseAddress(true);
        return s.getLocalPort();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to discover available port.", e);
    } finally {
        try {
            s.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

From source file:ch.vorburger.mariadb4j.DBConfigurationBuilder.java

protected int detectFreePort() {
    try {//from   w ww . java  2 s.c o m
        ServerSocket ss = new ServerSocket(0);
        port = ss.getLocalPort();
        ss.setReuseAddress(true);
        ss.close();
        return port;
    } catch (IOException e) {
        // This should never happen
        throw new RuntimeException(e);
    }
}

From source file:com.zotoh.maedr.device.TcpIO.java

private ServerSocket createSvrSockIt() throws IOException {

    InetAddress ip;/*from  www  . ja  va 2 s  .c  o m*/
    ip = isEmpty(_host) ? InetAddress.getLocalHost() : InetAddress.getByName(_host);

    ServerSocket soc = new ServerSocket(_port, _backlog, ip);
    ServerSocket s = null;
    try {
        soc.setReuseAddress(true);
        s = soc;
        soc = null;
    } finally {
        NetUte.close(soc);
    }

    tlog().debug("TCP: opened server socket: {} on host: {}", _port, _host);
    return s;
}

From source file:gridool.communication.transport.tcp.GridMasterSlaveWorkerServer.java

private AcceptorThread acceptConnections(final int port, final BlockingQueue<Socket> taskQueue,
        final PooledRequestHandler handler) throws GridException {
    final ServerSocket ss;
    try {/*from w w  w. j  a  va  2 s  . c  om*/
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
    } catch (IOException e) {
        String errmsg = "Could not create ServerSocket on port: " + port;
        LogFactory.getLog(getClass()).error(errmsg, e);
        throw new GridException(errmsg);
    }

    if (LOG.isInfoEnabled()) {
        LOG.info("GridMultiWorkersServer is started at port: " + port);
    }

    final int growThreshold = config.getReadThreadsGrowThreshold();
    final AcceptorThread acceptor = new AcceptorThread(ss, taskQueue, readerPool, growThreshold, handler);
    acceptor.start();
    return acceptor;
}

From source file:org.opennms.netmgt.xmlrpcd.XmlrpcAnticipator.java

public void setupWebServer() throws IOException {
    m_logger.info("XmlrpcAnticipator starting on port number " + m_port);

    m_webServer = new WebServer(m_port) {
        @Override/*from  w w  w . j a v a 2  s .c om*/
        protected ServerSocket createServerSocket(int port, int backlog, InetAddress addr) throws Exception {
            ServerSocket sock = new ServerSocket();
            sock.setReuseAddress(true);
            sock.bind(new InetSocketAddress(addr, port), backlog);
            return sock;
        }

    };
    m_webServer.addHandler("$default", this);
    m_webServer.start();
    waitForStartup();

    m_logger.info("XmlrpcAnticipator running on port number " + m_port);
}

From source file:IntergrationTest.OCSPIntegrationTest.java

/**
 * Method to test a port is available./*from ww  w  .j ava2s .c o  m*/
 *
 * @param port
 *
 * @return
 */
private boolean available(int port) {
    if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }
        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }
    return false;
}

From source file:com.endpoint.lg.browser.service.BrowserInstance.java

/**
 * Finds an available debug port//w  ww.  ja v  a 2s  .  co  m
 *
 * This could fail, if we find a port and something steals it before we get
 * a browser listening on it. This seems unlikely. Note that chromium
 * doesn't fail if its assigned debug port is unavailable (at least so far
 * as I can tell)
 */
private int findDebugPort() {
    int debugPort = 0;
    ServerSocket s = null;

    for (int i = MIN_DEBUG_PORT; i < MAX_DEBUG_PORT; i++) {
        try {
            s = new ServerSocket(i);
            s.setReuseAddress(true);
            debugPort = i;
        } catch (IOException e) {
            // Port isn't available
            getLog().debug("Port " + i + " isn't available");
        } finally {
            try {
                if (s != null) {
                    s.close();
                }
            } catch (IOException e) {
                // s wasn't opened. Don't throw this
            }
        }
        if (debugPort != 0)
            break;
    }
    if (debugPort == 0) {
        getLog().error("Couldn't find unused debug port for new browser activity");
        return 0;
    }
    getLog().debug("Found debug port " + debugPort + " for new browser instance");
    return debugPort;
}

From source file:com.evolveum.midpoint.repo.sql.SqlRepositoryFactory.java

private void checkPort(int port) throws RepositoryServiceFactoryException {
    if (port >= 65635 || port < 0) {
        throw new RepositoryServiceFactoryException("Port must be in range 0-65634, not '" + port + "'.");
    }/*from  ww  w  . j  a  v  a2  s  .c o m*/

    ServerSocket ss = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
    } catch (BindException e) {
        throw new RepositoryServiceFactoryException("Configured port (" + port + ") for H2 already in use.", e);
    } catch (IOException e) {
        LOGGER.error("Reported IO error, while binding ServerSocket to port " + port
                + " used to test availability " + "of port for H2 Server", e);
    } finally {
        try {
            if (ss != null) {
                ss.close();
            }
        } catch (IOException ex) {
            LOGGER.error("Reported IO error, while closing ServerSocket used to test availability "
                    + "of port for H2 Server", ex);
        }
    }
}