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:org.superbiz.moviefun.MoviesHtmlUnitTest.java

@BeforeClass
public static void start() throws IOException {

    // get a random unused port to use for http requests
    ServerSocket server = new ServerSocket(0);
    port = server.getLocalPort();//from   w w  w . j  av a2 s . co  m
    server.close();

    webApp = createWebApp();
    Properties p = new Properties();
    p.setProperty(EJBContainer.APP_NAME, "moviefun");
    p.setProperty(EJBContainer.PROVIDER, "tomee-embedded"); // need web feature
    p.setProperty(EJBContainer.MODULES, webApp.getAbsolutePath());
    p.setProperty(EmbeddedTomEEContainer.TOMEE_EJBCONTAINER_HTTP_PORT, String.valueOf(port));
    container = EJBContainer.createEJBContainer(p);
}

From source file:Main.java

/**
 * Safely close a server socket, absorbing exceptions and handling
 * <code>null</code> graciously.
 * /*from  w w  w.  j  a  v  a2  s.c  o m*/
 * @param socket object to close.
 */
public static void safeClose(ServerSocket socket) {
    try {
        if (socket != null) {
            socket.close();
        }
    } catch (IOException e) {
        log(socket, e);
    }
}

From source file:com.alibaba.jstorm.utils.NetWorkUtils.java

/**
 * Check whether the port is available to binding
 * //from   w  ww.ja  v  a2 s .c  om
 * @param port
 * @return -1 means not available, others means available
 * @throws IOException
 */
public static int tryPort(int port) throws IOException {
    ServerSocket socket = new ServerSocket(port);
    int rtn = socket.getLocalPort();
    socket.close();
    return rtn;
}

From source file:Main.java

public static boolean portIsUsing(int port) {
    ServerSocket serverSocket = null;
    try {// w w  w .ja  v a  2 s. c om
        serverSocket = new ServerSocket(port);
    } catch (IOException ex) {
        return true;
    }
    try {
        serverSocket.close();
    } catch (IOException ex) {
    }
    return false;
}

From source file:org.sonar.plugins.email.EmailSendingTest.java

private static int getNextAvailablePort() {
    try {//from   w w w  .  j a v  a  2 s. co m
        ServerSocket socket = new ServerSocket(0);
        int unusedPort = socket.getLocalPort();
        socket.close();
        return unusedPort;
    } catch (IOException e) {
        throw new RuntimeException("Error getting an available port from system", e);
    }
}

From source file:com.barchart.netty.rest.client.TestRestClientBase.java

@BeforeClass
public static void init() throws Exception {

    final ServerSocket s = new ServerSocket(0);
    port = s.getLocalPort();/*w  ww. j av a2s .c om*/
    s.close();

    handler = new TestRequestHandler();

    server = new HttpServer().requestHandler("/test", handler);

    server.listen(port).sync();

    client = new RestClientBase("http://localhost:" + port) {
    };

}

From source file:org.apache.cloudstack.network.contrail.management.TestDbSetup.java

public static int findFreePort() throws Exception {

    int port;//from  www. ja v  a  2 s .c om
    ServerSocket socket = new ServerSocket(0);
    port = socket.getLocalPort();
    socket.close();

    return port;
}

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

static int randomAvailablePort(int from, int to) {
    int repeat = 5;
    while (repeat > 0)
        try {/*  w  w  w  .ja v a  2  s .com*/
            int port = ThreadLocalRandom.current().nextInt(from, to + 1);
            ServerSocket socket = new ServerSocket(port);
            socket.close();
            return port;
        } catch (IOException ignored) {
            repeat--;
        }
    throw new RuntimeException("no free port found");
}

From source file:org.glowroot.agent.plugin.netty.NettyIT.java

private static int getAvailablePort() throws Exception {
    ServerSocket serverSocket = new ServerSocket(0);
    int port = serverSocket.getLocalPort();
    serverSocket.close();
    return port;/*from   w ww  .  j av a2 s  .  co m*/
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.hl7.core.InboundHL7IOReactor.java

private static boolean isPortAvailable(int port) {
    try {/*from w w w  .j a  v a  2s.  co  m*/
        ServerSocket ss = new ServerSocket(port);
        ss.close();
        return true;
    } catch (IOException e) {
        return false;
    }
}