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:com.nesscomputing.service.discovery.client.TestServiceDiscovery.java

private static final int findUnusedPort() {
    int port;// w ww. j a  v  a 2s  .com

    ServerSocket socket = null;
    try {
        socket = new ServerSocket();
        socket.bind(new InetSocketAddress(0));
        port = socket.getLocalPort();
    } catch (IOException ioe) {
        throw Throwables.propagate(ioe);
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ioe) {
            // GNDN
        }
    }

    return port;
}

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

/**
 * @return a random available port for binding
 *///from   ww  w .  j a  va 2 s  .  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:com.github.avarabyeu.restendpoint.http.GuiceTestModule.java

private static int findFreePort() {
    ServerSocket socket = null;
    try {/*from w w  w.jav  a  2 s . c om*/
        socket = new ServerSocket(0);
        System.out.println("Used Port: " + socket.getLocalPort());
        return socket.getLocalPort();
    } catch (IOException e) {
        throw new RuntimeException("Unable to find free port", e);
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
}

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();//  ww  w.ja  va  2s  .  c om
    return port;
}

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();//  w w  w . ja v  a  2s .  co  m
    return port;
}

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

public static int findFreePort() {
    int port = 0;
    try {//from  ww w  . ja  v  a2  s  .c o  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:Main.java

/**
 * Renders the details of a server socket in the returned string
 * @param socket The server socket to render
 * @return the details of the server socket as a string
 *//*from  w w w . j a  v a2 s .  c  o m*/
public static String render(ServerSocket socket) {
    if (socket == null)
        return "NULL";
    StringBuilder b = new StringBuilder("\nSocket [");
    b.append("\n\tLocalPort:").append(socket.getLocalPort());
    b.append("\n\tLocalAddress:").append(socket.getInetAddress());
    b.append("\n\tLocalSocketAddress:").append(socket.getLocalSocketAddress());
    b.append("\n\tChannel:").append(socket.getChannel());
    b.append("\n\tHashCode:").append(socket.hashCode());
    b.append("\n]");
    return b.toString();
}

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

private static int findUnusedPort() throws IOException {
    int port;//from ww w . j ava 2  s.  c o m

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

    return port;
}

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

public static int findFreePort() {
    ServerSocket server;
    try {// w ww.j  a  v  a  2s .c  o 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:org.apache.sentry.binding.hive.TestHiveAuthzBindingHookBase.java

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