Example usage for java.net ServerSocket ServerSocket

List of usage examples for java.net ServerSocket ServerSocket

Introduction

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

Prototype

public ServerSocket(int port) throws IOException 

Source Link

Document

Creates a server socket, bound to the specified port.

Usage

From source file:org.apache.flink.runtime.taskmanager.TaskManager.java

/**
 * Searches for an available free port and returns the port number.
 * /*w w w.j av a2s.  c o m*/
 * @return An available port.
 * @throws RuntimeException Thrown, if no free port was found.
 */
private static int getAvailablePort() {
    for (int i = 0; i < 50; i++) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(0);
            int port = serverSocket.getLocalPort();
            if (port != 0) {
                return port;
            }
        } catch (IOException e) {

            LOG.debug("Unable to allocate port with exception {}", e);
        } finally {
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (Throwable t) {
                }
            }
        }
    }

    throw new RuntimeException("Could not find a free permitted port on the machine.");
}

From source file:Tcpbw100.java

public boolean test_sfw(Protocol ctl) throws IOException {
    Message msg = new Message();
    if ((tests & TEST_SFW) == TEST_SFW) {
        showStatus(messages.getString("sfwTest"));
        results.append(messages.getString("checkingFirewalls") + "  ");
        statistics.append(messages.getString("checkingFirewalls") + "  ");
        emailText = messages.getString("checkingFirewalls") + "  ";
        pub_status = "checkingFirewalls";

        if (ctl.recv_msg(msg) != 0) {
            errmsg = messages.getString("protocolError") + Integer.parseInt(new String(msg.body), 16)
                    + " instead\n";
            return true;
        }/*from   www . j  av a  2s  . c o  m*/
        if (msg.type != TEST_PREPARE) {
            errmsg = messages.getString("sfwWrongMessage") + "\n";
            if (msg.type == MSG_ERROR) {
                errmsg += "ERROR MSG: " + Integer.parseInt(new String(msg.body), 16) + "\n";
            }
            return true;
        }

        String message = new String(msg.body);

        int srvPort, testTime;
        try {
            int k = message.indexOf(" ");
            srvPort = Integer.parseInt(message.substring(0, k));
            testTime = Integer.parseInt(message.substring(k + 1));
        } catch (Exception e) {
            errmsg = messages.getString("sfwWrongMessage") + "\n";
            return true;
        }

        System.out.println("SFW: port=" + srvPort);
        System.out.println("SFW: testTime=" + testTime);

        ServerSocket srvSocket;
        try {
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                System.out.println("Asking security manager for listen permissions...");
                security.checkListen(0);
            }
            srvSocket = new ServerSocket(0);
        } catch (Exception e) {
            e.printStackTrace();
            errmsg = messages.getString("sfwSocketFail") + "\n";
            return true;
        }

        System.out.println("SFW: oport=" + srvSocket.getLocalPort());
        ctl.send_msg(TEST_MSG, Integer.toString(srvSocket.getLocalPort()).getBytes());

        if (ctl.recv_msg(msg) != 0) {
            errmsg = messages.getString("protocolError") + Integer.parseInt(new String(msg.body), 16)
                    + " instead\n";
            return true;
        }
        if (msg.type != TEST_START) {
            errmsg = messages.getString("sfwWrongMessage");
            if (msg.type == MSG_ERROR) {
                errmsg += "ERROR MSG: " + Integer.parseInt(new String(msg.body), 16) + "\n";
            }
            return true;
        }

        OsfwWorker osfwTest = new OsfwWorker(srvSocket, testTime);
        new Thread(osfwTest).start();

        Socket sfwSocket = new Socket();
        try {
            sfwSocket.connect(new InetSocketAddress(host, srvPort), testTime * 1000);

            Protocol sfwCtl = new Protocol(sfwSocket);
            sfwCtl.send_msg(TEST_MSG, new String("Simple firewall test").getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (ctl.recv_msg(msg) != 0) {
            errmsg = messages.getString("protocolError") + Integer.parseInt(new String(msg.body), 16)
                    + " instead\n";
            return true;
        }
        if (msg.type != TEST_MSG) {
            errmsg = messages.getString("sfwWrongMessage") + "\n";
            if (msg.type == MSG_ERROR) {
                errmsg += "ERROR MSG: " + Integer.parseInt(new String(msg.body), 16) + "\n";
            }
            return true;
        }
        c2sResult = Integer.parseInt(new String(msg.body));

        osfwTest.finalize();

        if (ctl.recv_msg(msg) != 0) {
            errmsg = messages.getString("protocolError") + Integer.parseInt(new String(msg.body), 16)
                    + " instead\n";
            return true;
        }
        if (msg.type != TEST_FINALIZE) {
            errmsg = messages.getString("sfwWrongMessage") + "\n";
            if (msg.type == MSG_ERROR) {
                errmsg += "ERROR MSG: " + Integer.parseInt(new String(msg.body), 16) + "\n";
            }
            return true;
        }
        results.append(messages.getString("done") + "\n");
        statistics.append(messages.getString("done") + "\n");
        emailText += messages.getString("done") + "\n%0A";
    }
    return false;
}

From source file:com.chinamobile.bcbsp.workermanager.WorkerManager.java

@Override
public synchronized int getFreePort() {
    ServerSocket s;/*from  www.  j a  v  a 2  s .c  om*/
    this.currentFreePort = this.currentFreePort + 1;
    int count = 0;
    for (; this.currentFreePort <= 65536; this.currentFreePort++) {
        count++;
        if (count > 5535) {
            LOG.info("[WorkerManager: getFreePort()] attempts " + "to get a free port over 5535 times!");
            return 60000;
        }
        if (this.currentFreePort > 65535) {
            this.currentFreePort = 60001;
        }
        try {
            LOG.info("debug:this.currentFreePort is " + this.currentFreePort);
            s = new ServerSocket(this.currentFreePort);
            s.close();
            return this.currentFreePort;
        } catch (IOException e) {
            LOG.info("debug:this.currentFreePort is " + this.currentFreePort);
            LOG.error("[WokerManager] caught", e);
        }
    }
    return 60000;
}