Example usage for java.net ServerSocket getLocalPort

List of usage examples for java.net ServerSocket getLocalPort

Introduction

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

Prototype

public int getLocalPort() 

Source Link

Document

Returns the port number on which this socket is listening.

Usage

From source file:org.codice.ddf.catalog.ui.metacard.QueryMetacardApplicationTest.java

private static int getAvailablePort() {
    ServerSocket socket = null;
    try {//w  w  w. ja  va  2  s  .c  o m
        socket = new ServerSocket(0);
        return socket.getLocalPort();
    } catch (IOException e) {
        throw new AssertionError("Could not autobind to available port", e);
    } finally {
        tryCloseSocket(socket);
    }
}

From source file:sf.net.experimaestro.manager.js.SSHServer.java

/**
 * Finds a free local socket port.//w ww  . j  a v a2  s  .  c o  m
 *
 * @return a free local socket port.
 * @throws java.io.IOException
 */
public static int findFreeLocalPort() throws IOException {
    ServerSocket server = new ServerSocket(0);
    try {
        return server.getLocalPort();
    } finally {
        server.close();
    }
}

From source file:org.apache.beam.sdk.io.hadoop.format.HadoopFormatIOElasticTest.java

@BeforeClass
public static void startServer() throws NodeValidationException, IOException {
    ServerSocket serverSocket = new ServerSocket(0);
    int port = serverSocket.getLocalPort();
    serverSocket.close();/*from w  w w.  ja va  2s.c  o  m*/
    elasticInMemPort = String.valueOf(port);
    ElasticEmbeddedServer.startElasticEmbeddedServer();
}

From source file:org.apache.hama.MiniBSPCluster.java

private static void randomPort(HamaConfiguration conf) {
    try {// www.j ava2 s . c  om
        ServerSocket skt = new ServerSocket(0);
        int p = skt.getLocalPort();
        skt.close();
        conf.set(Constants.PEER_PORT, new Integer(p).toString());
        conf.setInt(Constants.GROOM_RPC_PORT, p + 100);
    } catch (IOException ioe) {
        LOG.error("Can not find a free port for BSPPeer.", ioe);
    }
}

From source file:de.undercouch.gradle.tasks.download.TestBase.java

/**
 * Find a free socket port/* www  .  jav a  2  s .  c  o  m*/
 * @return the number of the free port
 * @throws IOException if an IO error occurred
 */
protected static int findPort() throws IOException {
    ServerSocket socket = null;
    try {
        socket = new ServerSocket(0);
        return socket.getLocalPort();
    } finally {
        if (socket != null) {
            socket.close();
        }
    }
}

From source file:org.kiji.schema.cassandra.TestingCassandraFactory.java

/**
 * Find an available port./*  w ww.ja v a  2  s  .co  m*/
 *
 * @return an open port number.
 * @throws IllegalArgumentException if it can't find an open port.
 */
private static int findOpenPort() {
    try {
        ServerSocket serverSocket = new ServerSocket(0);
        int portNumber = serverSocket.getLocalPort();
        serverSocket.setReuseAddress(true);
        serverSocket.close();
        LOG.debug("Found usable port {}", portNumber);
        return portNumber;
    } catch (IOException ioe) {
        throw new RuntimeException("Could not find open port.");
    }
}

From source file:org.qi4j.library.shiro.AbstractServletTestSupport.java

protected static int findFreePortOnIfaceWithPreference(final InetAddress address, final int prefered)
        throws IOException {
    ServerSocket server;
    if (prefered > 0) {
        server = new ServerSocket(prefered, 1, address);
    } else {//from w  w w.j a  va 2  s  .c  o m
        server = new ServerSocket(0, 1, address);
    }
    int port = server.getLocalPort();
    server.close();
    return port;
}

From source file:com.twosigma.beakerx.kernel.MagicKernelManager.java

public static Integer findFreePort() {
    ServerSocket socket = null;
    try {/*  w  ww .j ava2 s.  c  o m*/
        socket = new ServerSocket(0);
        socket.setReuseAddress(true);
        int port = socket.getLocalPort();
        try {
            socket.close();
        } catch (IOException e) {
        }
        return port;
    } catch (IOException e) {
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}

From source file:org.apache.sshd.PortForwardingTest.java

private static int getFreePort() throws Exception {
    ServerSocket s = new ServerSocket(0);
    try {/*  w ww .  j  a v a2s  . c om*/
        return s.getLocalPort();
    } finally {
        s.close();
    }
}

From source file:org.zodiark.publisher.PublisherTest.java

public final static int findFreePort() {
    ServerSocket socket = null;

    try {//from   ww  w  . ja v a 2 s .c om
        socket = new ServerSocket(0);

        return socket.getLocalPort();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return 8080;
}