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.bonitasoft.connectors.email.test.EmailConnectorTest.java

private static boolean isFreePort(final int port) {
    try {//w w w. j  a va2s  .  co  m
        final ServerSocket socket = new ServerSocket(port);
        socket.close();
        return true;
    } catch (final IOException e) {
        return false;
    }
}

From source file:AvailablePortFinder.java

/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 *//* w  w w  . j  a  v  a  2  s.c  o  m*/
public static boolean available(int port) {
    if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

From source file:org.apache.tajo.rpc.NettyServerBase.java

private static boolean available(int port) throws IOException {
    if (port < 1024 || port > 65535) {
        throw new IllegalArgumentException("Port Number Out of Bound: " + port);
    }/*from ww  w  .  j  a va  2 s . co m*/

    ServerSocket ss = null;
    DatagramSocket ds = null;

    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);

        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);

        return true;

    } catch (IOException e) {
        return false;
    } finally {
        if (ss != null) {
            ss.close();
        }

        if (ds != null) {
            ds.close();
        }
    }
}

From source file:com.centeractive.ws.server.SimpleServerTest.java

public static boolean isPortAvailable(int port) {
    ServerSocket ss = null;
    DatagramSocket ds = null;//from   w  w w  .  j  ava  2s .co m
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
            }
        }
    }
    return false;
}

From source file:org.onosproject.incubator.rpc.grpc.GrpcRemoteServiceTest.java

public static int pickListenPort() {
    try {/*  www. ja  v  a 2  s  . c  o  m*/
        // pick unused port
        ServerSocket socket = new ServerSocket(0);
        int port = socket.getLocalPort();
        socket.close();
        return port;
    } catch (IOException e) {
        // something went wrong, try picking randomly
        return RandomUtils.nextInt(49152, 0xFFFF + 1);
    }
}

From source file:org.exoplatform.addons.es.integration.BaseIntegrationTest.java

/**
 * Get a random available port//ww w .j a  va  2 s .  c  om
 * @return
 * @throws IOException
 */
private static int getAvailablePort() throws IOException {
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(0);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(0);
        ds.setReuseAddress(true);
        return ss.getLocalPort();
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }
}

From source file:it.geosolutions.geostore.services.rest.SecurityTest.java

/**
 * Checks if a network host / port is already occupied.
 * //w w w.java2  s.c  o  m
 * @param host
 * @param port
 * @return
 */
private static boolean portIsBusy(String host, int port) {
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return false;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }
        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }
    return true;
}

From source file:com.twosigma.beaker.core.Main.java

private static boolean portAvailable(int port) {

    ServerSocket ss = null;
    try {/*  w  w  w. j av  a2 s .  c  om*/
        InetAddress address = InetAddress.getByName("127.0.0.1");
        ss = new ServerSocket(port, 1, address);
        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:org.apache.jorphan.util.JOrphanUtils.java

/**
 * close a Socket with no error thrown// w w w  .ja v a2s. c o m
 * @param sock - ServerSocket (may be null)
 */
public static void closeQuietly(ServerSocket sock) {
    try {
        if (sock != null) {
            sock.close();
        }
    } catch (IOException ignored) {
        // NOOP
    }
}

From source file:fr.fastconnect.factory.tibco.bw.maven.bwengine.AbstractServiceEngineMojo.java

public static boolean available(int port, int minPort, int maxPort) {
    if (port < minPort || port > maxPort || port > 65535) {
        throw new IllegalArgumentException("Invalid start port: " + port);
    }/*from ww  w  .  j  a va 2 s .  co m*/

    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}