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.apache.hadoop.hbase.TestIPv6NIOServerSocketChannel.java

/**
 * Creates a NIO ServerSocketChannel, and gets the ServerSocket from
 * there. Then binds the obtained socket.
 * This fails on Windows with Oracle JDK1.6.0u33, if the passed InetAddress is a
 * IPv6 address. Works on Oracle JDK 1.7.
 *//*from  www  .ja v  a2  s  .  c o m*/
private void bindNIOServerSocket(InetAddress inetAddr) throws IOException {
    while (true) {
        int port = HBaseTestingUtility.randomFreePort();
        InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
        ServerSocketChannel channel = null;
        ServerSocket serverSocket = null;
        try {
            channel = ServerSocketChannel.open();
            serverSocket = channel.socket();
            serverSocket.bind(addr); // This does not work
            break;
        } catch (BindException ex) {
            //continue
        } finally {
            if (serverSocket != null) {
                serverSocket.close();
            }
            if (channel != null) {
                channel.close();
            }
        }
    }
}

From source file:NanoHTTPD.java

private static final void safeClose(ServerSocket closeable) {
    if (closeable != null) {
        try {/*from   w  ww . j  a  v  a 2 s .c o m*/
            closeable.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.apache.activemq.leveldb.test.ReplicatedLevelDBBrokerTest.java

@Before
public void findFreePort() throws Exception {
    ServerSocket socket = new ServerSocket(0);
    port = socket.getLocalPort();//from w w w  .j  a v  a2 s.co  m
    socket.close();
}

From source file:org.apache.hadoop.ipc.TestServer.java

@Test
public void testBindError() throws Exception {
    Configuration conf = new Configuration();
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    socket.bind(address);//from w ww  .  ja  v  a2  s  .c  om
    try {
        int min = socket.getLocalPort();
        conf.set("TestRange", min + "-" + min);

        ServerSocket socket2 = new ServerSocket();
        InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
        boolean caught = false;
        try {
            Server.bind(socket2, address2, 10, conf, "TestRange");
        } catch (BindException e) {
            caught = true;
        } finally {
            socket2.close();
        }
        assertTrue("Failed to catch the expected bind exception", caught);
    } finally {
        socket.close();
    }
}

From source file:org.apache.nifi.atlas.emulator.EmbeddedKafka.java

/**
 * Will determine the available port used by Kafka/Zookeeper servers.
 *//*from  w  w w.ja  v a  2s  .  c om*/
private int availablePort() {
    ServerSocket s = null;
    try {
        s = new ServerSocket(0);
        s.setReuseAddress(true);
        return s.getLocalPort();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to discover available port.", e);
    } finally {
        try {
            s.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

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

private int findFreePort(int startPort) {
    int port = startPort;
    while (true) {
        port++;/* ww w .j av a  2s  .  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:org.eredlab.g4.ccl.net.bsd.RExecClient.java

InputStream _createErrorStream() throws IOException {
    ServerSocket server;
    Socket socket;/* ww w .j a va2 s. c o  m*/

    server = _socketFactory_.createServerSocket(0, 1, getLocalAddress());

    _output_.write(Integer.toString(server.getLocalPort()).getBytes());
    _output_.write('\0');
    _output_.flush();

    socket = server.accept();
    server.close();

    if (__remoteVerificationEnabled && !verifyRemote(socket)) {
        socket.close();
        throw new IOException("Security violation: unexpected connection attempt by "
                + socket.getInetAddress().getHostAddress());
    }

    return (new SocketInputStream(socket, socket.getInputStream()));
}

From source file:org.docwhat.iated.AppState.java

/** Find a free TCP port.
 *
 * From:/*from   ww  w. j  a  va  2  s.  c  o  m*/
 * http://stackoverflow.com/questions/2675362/how-to-find-an-available-port
 * http://stackoverflow.com/questions/3265825/finding-two-free-tcp-ports
 *
 * @return A free socket number.
 * @throws IOException If not free port can be found.
 */
public int findFreePort() throws IOException {
    ServerSocket socket = null;

    try {
        socket = new ServerSocket(0);
        return socket.getLocalPort();
    } catch (Exception e) {
        throw new IOException("no free port found");
    } finally {
        if (socket != null) {
            socket.close();
        }
    }
}

From source file:org.apache.lens.server.LensJerseyTest.java

protected int getTestPort() {
    if (!isPortAlreadyFound()) {
        return port;
    }/* ww  w  . j a  v a  2s  .  c  o m*/
    ServerSocket socket = null;
    try {
        socket = new ServerSocket(0);
        setPort(socket.getLocalPort());
    } catch (IOException e) {
        LOG.info("Exception occured while creating socket. Use a default port number " + port);
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
            LOG.info("Exception occured while closing the socket ", e);
        }
    }
    return port;
}

From source file:ezbake.thriftrunner.starters.SimpleStarter.java

boolean serverSocketIsFree(int port) {
    ServerSocket socket = null;
    try {// w  w w. j  a v  a2 s  .  c  om
        socket = new ServerSocket();
        socket.setReuseAddress(true);
        socket.bind(new InetSocketAddress(port));
        this.portNumber = socket.getLocalPort();
        return true;
    } catch (final IOException e) {
        return false;
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (final IOException e) {
            // should never happen
        }
    }
}