Example usage for java.net ServerSocket isClosed

List of usage examples for java.net ServerSocket isClosed

Introduction

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

Prototype

public boolean isClosed() 

Source Link

Document

Returns the closed state of the ServerSocket.

Usage

From source file:com.aqnote.shared.cryptology.util.lang.StreamUtil.java

public static void close(ServerSocket ssocket) {
    try {// ww w  .j av  a 2 s . c o m
        if (ssocket != null && !ssocket.isClosed())
            ssocket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.yahoo.druid.hadoop.OverlordTestServer.java

private int findFreePort(int startPort) {
    int port = startPort;
    while (true) {
        port++;/*from  w w  w .  j a v  a  2 s .  c  o  m*/
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(port);
            return port;
        } catch (BindException be) {
            //port in use
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (ss != null) {
                while (!ss.isClosed()) {
                    try {
                        ss.close();
                    } catch (IOException e) {
                        // ignore
                    }
                }
            }
        }
    }
}

From source file:com.reversemind.hypergate.server.HyperGateServer.java

/**
 * Detect free setPort on System//  w  w w .java2s  .com
 *
 * @return
 */
private int detectFreePort() {
    try {
        ServerSocket serverSocket = new ServerSocket(0);
        if (serverSocket.getLocalPort() == -1) {
            System.exit(-100);
            throw new RuntimeException(
                    "\n\nCould not start HyperGateServer there are no any free port in the system available");
        }

        int detectedPortNumber = serverSocket.getLocalPort();

        serverSocket.close();
        int count = 0;
        while (!serverSocket.isClosed()) {
            if (count++ > 10) {
                throw new RuntimeException("Could not start HyperGateServer");
            }
            try {
                Thread.sleep(100);
                LOG.info("Waiting for closing auto discovered socket try number#" + count);
            } catch (InterruptedException e) {
                System.exit(-100);
                throw new RuntimeException("Could not start HyperGateServer");
            }
        }
        serverSocket = null;

        return detectedPortNumber;
    } catch (IOException e) {
        e.printStackTrace();
    }
    throw new RuntimeException("Could not start HyperGateServer 'cause no any available free port in system");
}

From source file:net.sf.ehcache.distribution.RMICacheManagerPeerListener.java

/**
 * Gets a free server socket port.//from ww  w. ja  v  a  2  s  .c  o  m
 *
 * @return a number in the range 1025 - 65536 that was free at the time this method was executed
 * @throws IllegalArgumentException
 */
protected int getFreePort() throws IllegalArgumentException {
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(0);
        return serverSocket.getLocalPort();
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not acquire a free port number.");
    } finally {
        if (serverSocket != null && !serverSocket.isClosed()) {
            try {
                serverSocket.close();
            } catch (Exception e) {
                LOG.debug("Error closing ServerSocket: " + e.getMessage());
            }
        }
    }
}

From source file:eu.stratosphere.nephele.taskmanager.TaskManager.java

private int getAvailablePort() {
    ServerSocket serverSocket = null;
    int port = 0;
    for (int i = 0; i < 50; i++) {
        try {/*from  w  w  w .  j a va  2 s .c o  m*/
            serverSocket = new ServerSocket(0);
            port = serverSocket.getLocalPort();
            if (port != 0) {
                serverSocket.close();
                break;
            }
        } catch (IOException e) {
            LOG.debug("Unable to allocate port " + e.getMessage(), e);
        }
    }
    if (!serverSocket.isClosed()) {
        try {
            serverSocket.close();
        } catch (IOException e) {
            LOG.debug("error closing port", e);
        }
    }
    return port;
}

From source file:org.echocat.jomon.net.dns.DnsServer.java

@Nonnull
private Socket accept(@Nonnull ServerSocket sock) throws IOException {
    try {//from  ww w .j  a  va  2  s.  c  o  m
        return sock.accept();
    } catch (final SocketException e) {
        if (sock.isClosed()) {
            final InterruptedIOException toThrow = new InterruptedIOException();
            toThrow.initCause(e);
            throw toThrow;
        } else {
            throw e;
        }
    }
}

From source file:VASSAL.tools.io.IOUtilsTest.java

@Test
public void testCloseQuietlyServerSocketOpen() throws IOException {
    final ServerSocket s = new ServerSocket(0);
    assertFalse(s.isClosed());
    IOUtils.closeQuietly(s);//  www. j  av a 2  s. com
    assertTrue(s.isClosed());
}

From source file:VASSAL.tools.io.IOUtilsTest.java

@Test
public void testCloseQuietlyServerSocketClosed() throws IOException {
    final ServerSocket s = new ServerSocket(0);
    s.close();/*from w  w  w . ja va2  s . c om*/
    assertTrue(s.isClosed());
    IOUtils.closeQuietly(s);
    assertTrue(s.isClosed());
}