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:de.stklcode.jvault.connector.HTTPVaultConnectorTest.java

/**
 * Find and return a free TCP port.//from  ww  w  . ja  v a2  s.  c  o  m
 *
 * @return port number
 */
private static Integer getFreePort() {
    ServerSocket socket = null;
    try {
        socket = new ServerSocket(0);
        socket.setReuseAddress(true);
        int port = socket.getLocalPort();
        try {
            socket.close();
        } catch (IOException e) {
            // Ignore IOException on close()
        }
        return port;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    throw new IllegalStateException("Unable to find a free TCP port.");
}

From source file:gridool.util.xfer.TransferServer.java

public void setup(int port) throws IOException {
    ServerSocket servSocket = serverChannel.socket();
    servSocket.setReuseAddress(true);
    InetAddress addr = NetUtils.getLocalHost(false);
    InetSocketAddress sockaddr = new InetSocketAddress(addr, port);
    servSocket.bind(sockaddr);//from  w w w .  j av a2  s  .  c o  m
}

From source file:gridool.util.xfer.TransferServer.java

/**
 * @return binded sock address// ww w . j a v  a  2s  .c  o  m
 */
public InetSocketAddress setup() throws IOException {
    ServerSocket servSocket = serverChannel.socket();
    servSocket.setReuseAddress(true);
    InetSocketAddress sockaddr = NetUtils.getAnyLocalInetSocketAddress();
    servSocket.bind(sockaddr);
    return sockaddr;
}

From source file:org.apache.hadoop.thriftfs.ThriftPluginServer.java

/**
 * Start processing requests.//from  w w w  .  j ava 2  s .  c  o m
 * 
 * @throws IllegalStateException if the server has already been started.
 * @throws IOException on network errors.
 */
public void start() throws IOException {
    String hostname = address.getAddress().getHostAddress();

    synchronized (this) {
        if (server != null) {
            throw new IllegalStateException("Thrift server already started");
        }
        LOG.info("Starting Thrift server");
        ServerSocket sock = new ServerSocket();
        sock.setReuseAddress(true);
        if (port == 0) {
            sock.bind(null);
            address = new InetSocketAddress(hostname, sock.getLocalPort());
            port = address.getPort();
        } else {
            sock.bind(address);
        }
        TServerTransport transport = new TServerSocket(sock, SOCKET_READ_TIMEOUT);
        SanerThreadPoolServer.Options options = new SanerThreadPoolServer.Options();
        options.minWorkerThreads = conf.getInt("dfs.thrift.threads.min", 5);
        options.maxWorkerThreads = conf.getInt("dfs.thrift.threads.max", 20);
        options.stopTimeoutVal = conf.getInt("dfs.thrift.timeout", 60);
        options.stopTimeoutUnit = TimeUnit.SECONDS;
        options.queueSize = conf.getInt("dfs.thrift.queue.size", 4 * options.maxWorkerThreads);

        server = new SanerThreadPoolServer(processorFactory, transport, new TTransportFactory(),
                new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), options);
    }

    Thread t = new Thread(this);
    t.start();
    LOG.info("Thrift server listening on " + hostname + ":" + port);
}

From source file:org.mule.tck.junit4.rule.FreePortFinder.java

/**
 * Check and log is a given port is available
 *
 * @param port the port number to check//from w  ww.j  a va2s . c  o m
 * @return true if the port is available, false otherwise
 */
public boolean isPortFree(int port) {
    boolean portIsFree = true;

    ServerSocket server = null;
    try {
        server = new ServerSocket(port);
        server.setReuseAddress(true);
    } catch (IOException e) {
        portIsFree = false;
    } finally {
        if (server != null) {
            try {
                server.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }

    return portIsFree;
}

From source file:org.apache.vysper.xmpp.extension.xep0124.inttests.IntegrationTestTemplate.java

private int findFreePort() throws IOException {
    ServerSocket ss = null;
    try {/*w w w.j  a v  a  2  s.  co  m*/
        ss = new ServerSocket(0);
        ss.setReuseAddress(true);
        return ss.getLocalPort();
    } finally {
        if (ss != null) {
            ss.close();
        }
    }
}

From source file:com.twosigma.beaker.core.rest.PluginServiceLocatorRest.java

private static boolean isPortAvailable(int port) {

    ServerSocket ss = null;
    try {//from   w w w  . java2  s. c om
        InetAddress address = InetAddress.getByName("127.0.0.1");
        ss = new ServerSocket(port, 1, address);
        // ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }
    return false;
}

From source file:com.seajas.search.utilities.rmi.RestrictedHostSocketFactory.java

/**
 * Create a server socket which only binds to the specified hostname.
 * /*from   w w  w  . j av  a 2s. co  m*/
 * @param port
 * @return ServerSocket
 * @throws IOException
 */
@Override
public ServerSocket createServerSocket(final int port) throws IOException {
    InetAddress inetAddress = null;

    if (logger.isInfoEnabled())
        logger.info(
                "Creating (server-side) socket #" + totalSocketsCreated.incrementAndGet() + " on port " + port);

    if (!StringUtils.isEmpty(hostname) && !hostname.equals("0.0.0.0"))
        inetAddress = InetAddress.getByName(hostname);

    ServerSocket result = new ServerSocket(port, DEFAULT_BACKLOG, inetAddress);

    result.setReuseAddress(true);

    return result;
}

From source file:org.openflamingo.remote.thrift.thriftfs.ThriftPluginServer.java

/**
 * Start processing requests./* w  ww.j a v a 2  s . c o  m*/
 *
 * @throws IllegalStateException if the server has already been started.
 * @throws java.io.IOException   on network errors.
 */
public void start() throws IOException {
    String hostname = address.getAddress().getHostAddress();

    synchronized (this) {
        if (server != null) {
            throw new IllegalStateException("Thrift server already started");
        }
        LOG.info("Starting Thrift server");
        ServerSocket sock = new ServerSocket();
        sock.setReuseAddress(true);
        if (port == 0) {
            sock.bind(null);
            address = new InetSocketAddress(hostname, sock.getLocalPort());
            port = address.getPort();
        } else {
            sock.bind(address);
        }

        int socketTimeout = conf.getInt("dfs.thrift.socket.timeout", SOCKET_READ_TIMEOUT);

        TServerTransport transport = new TServerSocket(sock, socketTimeout);
        SanerThreadPoolServer.Options options = new SanerThreadPoolServer.Options();
        options.minWorkerThreads = conf.getInt("dfs.thrift.threads.min", 5);
        options.maxWorkerThreads = conf.getInt("dfs.thrift.threads.max", 20);
        options.stopTimeoutVal = conf.getInt("dfs.thrift.timeout", 60);
        options.stopTimeoutUnit = TimeUnit.SECONDS;
        options.queueSize = conf.getInt("dfs.thrift.queue.size", 4 * options.maxWorkerThreads);

        server = new SanerThreadPoolServer(processorFactory, transport, transportFactory, transportFactory,
                new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), options);
    }

    Thread t = new Thread(this);
    t.start();
    LOG.info("Thrift server listening on " + hostname + ":" + port);
}

From source file:ezbake.thriftrunner.starters.SimpleStarter.java

boolean serverSocketIsFree(int port) {
    ServerSocket socket = null;
    try {//from  w ww  . j  a  v a2s . co  m
        socket = new ServerSocket();
        socket.setReuseAddress(true);
        socket.bind(new InetSocketAddress(port));
        this.portNumber = socket.getLocalPort();
        return true;
    } catch (final IOException e) {
        return false;
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (final IOException e) {
            // should never happen
        }
    }
}