1

I am developing eclipse plugin for our organization . We are opening multiple servers[minimum 10 servers] on a user machine using this plugin via eclipse . For starting servers we want port numbers which has been not already binded . For that , I am using serversocket to check this . I think it's a costly operation to open a serversocket object . Internally serversocket will check the port is already binded or not It takes minimum 50 milliseconds . Here is my code to return a free port . Is there any way to find already occupied ports without using OS Commands and opening ServerSocket ?

     /**
     *Tries 100 times
     * @param port
     * modes
     *  1.increment - 1
     *  This mode increment the port with your start value . But it's costly operation because each time we open a socket and check the port is free .
     *  2.decrement - 2
     *  Invert of increment.
     *  3.random - 3
     *  Randomly choose based on your starting point
     * @return
     */
    public static String getDefaultPort(int port , int mode){
        int retry = 100;
        int random = 3;
        int increment = 1;
        int decrement = 2;
        while(true){
            //this is for preventing stack overflow error.
            if(retry < 1){ //retries 100 times .
                break;
            }
            if(mode==increment){
                port++;
            }else if(mode == decrement){
                port--;
            }else if(mode == random){
                port = (int) (port+Math.floor((Math.random()*1000)));
            }
            if(validate(port+"")){
                long end = System.currentTimeMillis();
                return port+"";
            }
        }
        return "";
    }

    public boolean validate(String input) {
    boolean status = true;
    try {
        int port = Integer.parseInt(input);
        ServerSocket ss = new ServerSocket(port);
        ss.close();         
    }
    catch (Exception e) {
        status = false;
    }
    return status;
}
kannanrbk
  • 6,964
  • 13
  • 53
  • 94

1 Answers1

2

The quickest way would be to run native netstat command and parse the output. It's available on Windows as well and Linux platform. A typical netstat command output is as follows

 Proto  Local Address          Foreign Address        State
 TCP    MYHOST:8080           MYHOST.mydomain.co.in:0  LISTENING
 TCP    MYHOST:9090           MYHOST.mydomain.co.in:0  LISTENING
 TCP    MYHOST:3389           MYHOST.mydomain.co.in:0  LISTENING
 TCP    MYHOST:7717           MYHOST.mydomain.co.in:0  LISTENING
 TCP    MYHOST:51114          MYHOST.mydomain.co.in:0  LISTENING
 TCP    MYHOST:netbios-ssn    MYHOST.mydomain.co.in:0  LISTENING
 TCP    MYHOST:netbios-ssn    MYHOST.mydomain.co.in:0  LISTENING
 TCP    MYHOST:2573           hj-lyncfe.mydomain.co.in:5061  ESTABLISHED
 TCP    MYHOST:2591           mail.mydomain.co.in:8502  ESTABLISHED
 TCP    MYHOST:2593           mail.mydomain.co.in:8502  ESTABLISHED

The ports of your interest are in the column Local Address with State in LISTENING

Santosh
  • 17,667
  • 4
  • 54
  • 79