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.zaproxy.zap.extension.proxies.ExtensionProxies.java

protected boolean canListenOn(String address, int port) {
    ServerSocket socket = null;
    try {/*from   ww w  .j ava2s  .  com*/
        socket = new ServerSocket(port, 5, InetAddress.getByName(address));
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        if (socket != null)
            try {
                socket.close();
            } catch (IOException e) {
                // Ignore
            }
    }
}

From source file:com.nesscomputing.db.postgres.embedded.EmbeddedPostgreSQL.java

private int detectPort() throws IOException {
    final ServerSocket socket = new ServerSocket(0);
    try {//from   w w w  .j a va  2  s . c  o  m
        return socket.getLocalPort();
    } finally {
        socket.close();
    }
}

From source file:org.mule.test.firewall.FirewallTestCase.java

protected void doTestTcp(InetAddress address, int port) throws Exception {
    try {// w w  w  . jav  a  2  s . c om
        logger.debug("Testing TCP on " + addressToString(address, port));
        ServerSocket server = openTcpServer(address, port);
        Socket client = openTcpClient(address, port);
        Socket receiver = server.accept();
        client.getOutputStream().write(1);
        assertEquals("Failed to send byte via " + addressToString(address, port), 1,
                receiver.getInputStream().read());
        client.close();
        server.close();
    } catch (Exception e) {
        logger.error("Error while attempting TCP message on " + addressToString(address, port));
        throw e;
    }
}

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

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

        ServerSocket socket2 = new ServerSocket();
        InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
        Server.bind(socket2, address2, 10, conf, "TestRange");
        try {
            assertTrue(socket2.isBound());
            assertTrue(socket2.getLocalPort() > min);
            assertTrue(socket2.getLocalPort() <= max);
        } finally {
            socket2.close();
        }
    } finally {
        socket.close();
    }
}

From source file:org.wso2.carbon.identity.common.testng.CarbonBasedTestListener.java

/**
 * Initializes the micro-service server.
 * Detects an available port from the system and use that for the microservice server.
 *//*  ww w .  j a  v a  2  s  .c  om*/
private MicroserviceServer initMicroserviceServer(Object realInstance) throws TestCreationException {

    try {

        ServerSocket s = new ServerSocket(0);
        int port = s.getLocalPort();
        s.close();

        MicroserviceServer microserviceServer = new MicroserviceServer(port);
        microserviceServer.init();
        microserviceServerMap.put(realInstance, microserviceServer);
        return microserviceServer;

    } catch (IOException e) {
        throw new TestCreationException("Could not get an aviailable port for micro-service", e);
    }
}

From source file:org.apache.solr.update.DocumentProcessor.java

/**
 * Send the data/*www  .  j  a v a 2 s.c o m*/
 * @param data
 */
void serveData(String data) {
    try {
        ServerSocket srvr = new ServerSocket(thePort);
        System.out.println("DocumentProcessor socket " + srvr);
        Socket skt = srvr.accept();
        System.out.print("Server has connected!\n");
        PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
        System.out.print("Sending string: '" + data + "'\n");
        out.print(data);
        out.close();
        skt.close();
        srvr.close();
    } catch (Exception e) {
        System.out.print("Whoops! It didn't work!\n");
        e.printStackTrace();
        //TODO figure out how to get this into Solr's logging system
    }
}

From source file:net.sourceforge.cobertura.test.util.WebappServer.java

public WebappServer(File webappServerDir, boolean tomcat) {
    this.tomcat = tomcat;
    ServerSocket s;
    try {/* ww w . j  av a 2 s.c  o  m*/
        s = new ServerSocket(0);
        webappPort = s.getLocalPort();
        s.close();

        s = new ServerSocket(0);
        stopPort = s.getLocalPort();
        s.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.huahinframework.emanager.ShutDowner.java

@Override
public void run() {
    log.info("ShutDowner start");

    try {//from w w  w . java2s.c  om
        ServerSocket svsock = new ServerSocket(port);
        for (;;) {
            Socket socket = svsock.accept();
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line = reader.readLine();

            reader.close();
            socket.close();

            if (line.matches(OPERATION_STOP)) {
                server.stop();
                queueManager.terminate();
                break;
            }
        }

        svsock.close();
    } catch (Exception e) {
        log.error(e);
        e.printStackTrace();
    }

    log.info("ShutDowner end");
}

From source file:org.springframework.security.kerberos.test.MiniKdc.java

/**
 * Creates a MiniKdc.// ww w . j  a  va2  s.com
 *
 * @param conf MiniKdc configuration.
 * @param workDir working directory, it should be the build directory. Under
 *                this directory an ApacheDS working directory will be created,
 *                this directory will be deleted when the MiniKdc stops.
 * @throws Exception  thrown if the MiniKdc could not be created.
 */
public MiniKdc(Properties conf, File workDir) throws Exception {
    if (!conf.keySet().containsAll(PROPERTIES)) {
        Set<String> missingProperties = new HashSet<String>(PROPERTIES);
        missingProperties.removeAll(conf.keySet());
        throw new IllegalArgumentException("Missing configuration properties: " + missingProperties);
    }
    this.workDir = new File(workDir, Long.toString(System.currentTimeMillis()));
    if (!workDir.exists() && !workDir.mkdirs()) {
        throw new RuntimeException("Cannot create directory " + workDir);
    }
    LOG.info("Configuration:");
    LOG.info("---------------------------------------------------------------");
    for (Map.Entry<?, ?> entry : conf.entrySet()) {
        LOG.info("  {}: {}", entry.getKey(), entry.getValue());
    }
    LOG.info("  localhost hostname: {}", InetAddress.getLocalHost().getHostName());
    LOG.info("  localhost canonical hostname: {}", InetAddress.getLocalHost().getCanonicalHostName());
    LOG.info("---------------------------------------------------------------");
    this.conf = conf;
    port = Integer.parseInt(conf.getProperty(KDC_PORT));
    if (port == 0) {
        ServerSocket ss = new ServerSocket(0, 1, InetAddress.getByName(conf.getProperty(KDC_BIND_ADDRESS)));
        port = ss.getLocalPort();
        ss.close();
    }
    String orgName = conf.getProperty(ORG_NAME);
    String orgDomain = conf.getProperty(ORG_DOMAIN);
    realm = orgName.toUpperCase() + "." + orgDomain.toUpperCase();
}

From source file:org.ngrinder.perftest.service.ConsoleManagerTest.java

@Test
public void testAvailableSocket() throws IOException {
    ServerSocket serverSocket = null;
    try {/*from  w  ww .  j  a  va 2 s.c  o  m*/
        // When port is already used
        serverSocket = new ServerSocket(20111);
        int localPort = serverSocket.getLocalPort();

        // It should be excluded in available ports
        List<Integer> availablePorts = NetworkUtils.getAvailablePorts("", 20, 20111, 40000);
        assertThat(availablePorts.contains(localPort), not(true));
        assertThat(availablePorts.size(), is(20));
    } finally {
        if (serverSocket != null) {
            serverSocket.close();
        }
    }
}