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:com.talis.jersey.apitest.DefaultExceptionMapperAcceptanceTest.java

private int findFreePort() throws IOException {
    ServerSocket serverSocket = new ServerSocket(0);
    int localPort = serverSocket.getLocalPort();
    serverSocket.close();
    return localPort;
}

From source file:org.ow2.proactive.resourcemanager.node.jmx.SigarExposer.java

@Override
public int getJMXRMIConnectorServerPort() {
    ServerSocket server;
    try {// w ww. j av a  2s .  c  o m
        server = new ServerSocket(0);
        int port = server.getLocalPort();
        server.close();
        return port;
    } catch (Exception e) {
        LOGGER.error("", e);
    }

    // in worst case try to return a random port from the range 5000-6000
    return (int) (5000 + (Math.random() * 1000));
}

From source file:org.apache.cxf.dosgi.singlebundle.AggregatedActivatorTest.java

@Override
protected void setUp() throws Exception {
    oldDefaultPort = AggregatedActivator.DEFAULT_HTTP_PORT;
    // Change the default port to one that we know is available
    ServerSocket s = new ServerSocket(0);
    int availablePort = s.getLocalPort();
    s.close();
    AggregatedActivator.DEFAULT_HTTP_PORT = "" + availablePort;

    savedProps = new HashMap<Object, Object>(System.getProperties());
    super.setUp();
}

From source file:com.meyling.telnet.startup.TelnetD.java

/**
 * Method to prepare the PortListener.<br>
 *
 * Creates and prepares and runs a PortListener, with settings from the
 * passed in Properties. Yet the Listener will not accept any incoming
 * connections before startServing() has been called. this has the advantage
 * that whenever a TelnetD Singleton has been factorized, it WILL 99% not
 * fail any longer (e.g. serve its purpose).
 *
 * @param   name            Name of listner.
 * @param     settings        Properties object that holds main settings.
 * @throws     BootException    Preparation failed.
 *///w w  w.  ja v a 2 s.c  o m
private void prepareListener(final String name, final Properties settings) throws BootException {

    int port = 0;
    try {
        port = Integer.parseInt(settings.getProperty(name + ".port"));
        ServerSocket socket = new ServerSocket(port);
        socket.close();
    } catch (NumberFormatException e) {
        trace.fatal(e, e);
        throw new BootException(
                "Failure while parsing port number for \"" + name + ".port\": " + e.getMessage());
    } catch (IOException e) {
        trace.fatal(e, e);
        throw new BootException(
                "Failure while starting listener for port number " + port + ": " + e.getMessage());
    }
    // factorize PortListener
    final PortListener listener = PortListener.createPortListener(name, settings);
    // start the Thread derived PortListener
    try {
        listeners.add(listener);
    } catch (Exception e) {
        trace.fatal(e, e);
        throw new BootException("Failure while starting PortListener thread: " + e.getMessage());
    }

}

From source file:com.streamsets.datacollector.http.TestLogServlet.java

private int getRandomPort() throws Exception {
    ServerSocket ss = new ServerSocket(0);
    int port = ss.getLocalPort();
    ss.close();
    return port;/*from  www. j av a  2  s .c  o m*/
}

From source file:test.unit.be.e_contract.dssp.client.DigitalSignatureServiceClientTest.java

private int getFreePort() throws IOException {
    ServerSocket server = new ServerSocket(0);
    int port = server.getLocalPort();
    server.close();
    return port;/*  w ww . j a  v a  2  s . c o  m*/
}

From source file:org.springframework.security.ldap.server.ApacheDSContainerTests.java

private List<Integer> getDefaultPorts(int count) throws IOException {
    List<ServerSocket> connections = new ArrayList<ServerSocket>();
    List<Integer> availablePorts = new ArrayList<Integer>(count);
    try {/*from  w ww  . ja  v  a  2  s .c  om*/
        for (int i = 0; i < count; i++) {
            ServerSocket socket = new ServerSocket(0);
            connections.add(socket);
            availablePorts.add(socket.getLocalPort());
        }
        return availablePorts;
    } finally {
        for (ServerSocket conn : connections) {
            conn.close();
        }
    }
}

From source file:org.kercoin.magrit.Magrit.java

void tryBind(int port) throws IOException {
    ServerSocket ss = null;
    try {//  w ww .ja v  a  2s  .c o m
        ss = new ServerSocket(port);
    } finally {
        if (ss != null && ss.isBound()) {
            ss.close();
        }
    }
}

From source file:com.ning.arecibo.dao.MysqlTestingHelper.java

public MysqlTestingHelper() {
    // New socket on any free port
    final ServerSocket socket;
    try {//w w  w.ja v a  2  s  .c om
        socket = new ServerSocket(0);
        port = socket.getLocalPort();
        socket.close();
    } catch (IOException e) {
        Assert.fail();
    }
}

From source file:to.sven.androidrccar.host.communication.impl.SocketConnector.java

/**
 * Start to listen on the {@link ServerSocket}.
 *//*www  .  ja v a 2s  .  c  o m*/
@Override
protected AsyncTaskResult<Socket> doInBackground(Object... params) {
    try {
        ServerSocket serverSocket = new ServerSocket(port);
        Socket socket = serverSocket.accept();
        serverSocket.close();
        return new AsyncTaskResult<Socket>(socket);
    } catch (Exception ex) {
        return new AsyncTaskResult<Socket>(ex);
    }
}