Example usage for java.net ServerSocket close

List of usage examples for java.net ServerSocket close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this socket.

Usage

From source file:io.vertx.ext.consul.dc.ConsulAgent.java

private static int getFreePort() {
    int port = -1;
    try {/* w w w  .j  a va 2s. c o m*/
        ServerSocket socket = new ServerSocket(0);
        port = socket.getLocalPort();
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return port;
}

From source file:org.apache.hadoop.hive.hbase.HBaseTestSetup.java

private static int findFreePort() throws IOException {
    ServerSocket server = new ServerSocket(0);
    int port = server.getLocalPort();
    server.close();
    return port;/*from  ww  w.j  av  a2 s  .  c  o  m*/
}

From source file:io.crate.testing.Utils.java

/**
 * @return a random available port for binding
 *///from   w  w w .  j  a  v  a  2s . co  m
static int randomAvailablePort() {
    try {
        ServerSocket socket = new ServerSocket(0);
        int port = socket.getLocalPort();
        socket.close();
        return port;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:test.be.fedict.eid.applet.AppletTest.java

public static int getFreePort() throws Exception {
    ServerSocket serverSocket = new ServerSocket(0);
    int port = serverSocket.getLocalPort();
    serverSocket.close();
    return port;/*from w ww.j ava2  s . c  om*/
}

From source file:com.yeetor.util.Util.java

/**
 * ???/* w w w. j  a  v  a 2  s  .c  o m*/
 * @return
 */
public static int getFreePort() {
    ServerSocket tmp;
    int i = 10000;
    while (true) {
        try {

            i = RandomUtils.nextInt(10000, 65535);
            tmp = new ServerSocket(i);
            tmp.close();
            tmp = null;
            return i;
        } catch (Exception e4) {
            continue;
        }
    }
}

From source file:org.uberfire.java.nio.fs.jgit.AbstractTestInfra.java

public static int findFreePort() {
    int port = 0;
    try {//from w w  w. ja  va2 s  . co m
        ServerSocket server = new ServerSocket(0);
        port = server.getLocalPort();
        server.close();
    } catch (IOException e) {
        Assert.fail("Can't find free port!");
    }
    logger.debug("Found free port " + port);
    return port;
}

From source file:org.apache.hadoop.net.ServerSocketUtil.java

/**
 * Find the specified number of unique ports available.
 * The ports are all closed afterwards,//  w ww. j  a  v  a  2  s . c om
 * so other network services started may grab those same ports.
 *
 * @param numPorts number of required port nubmers
 * @return array of available port numbers
 * @throws IOException
 */
public static int[] getPorts(int numPorts) throws IOException {
    ServerSocket[] sockets = new ServerSocket[numPorts];
    int[] ports = new int[numPorts];
    for (int i = 0; i < numPorts; i++) {
        ServerSocket sock = new ServerSocket(0);
        sockets[i] = sock;
        ports[i] = sock.getLocalPort();
    }
    for (ServerSocket sock : sockets) {
        sock.close();
    }
    return ports;
}

From source file:org.apache.sentry.binding.hive.TestHiveAuthzBindingHookBase.java

private static int findPort() throws IOException {
    ServerSocket socket = new ServerSocket(0);
    int port = socket.getLocalPort();
    socket.close();
    return port;// w  ww . j  a v a  2  s  . c o  m
}

From source file:com.talis.platform.testsupport.StubHttp.java

public static int findFreePort() {
    ServerSocket server;
    try {//from  w ww  .ja  v  a2 s  . co m
        server = new ServerSocket(0);
        int port = server.getLocalPort();
        server.close();
        return port;
    } catch (IOException e) {
        throw new RuntimeException("IOException while trying to find a free port", e);
    }
}

From source file:com.nesscomputing.httpserver.TestGuiceModule.java

private static int findUnusedPort() throws IOException {
    int port;/*  w ww . ja  v a2  s  . co m*/

    ServerSocket socket = new ServerSocket();
    try {
        socket.bind(new InetSocketAddress(0));
        port = socket.getLocalPort();
    } finally {
        socket.close();
    }

    return port;
}