If you want to scan the ports on your machine and know on which ports servers are listening, use the code sample below. Alter the for loop according to the requirement.

Java Code:
    for (int i = 0; i < 2000; i++) {
      try {
        System.out.println("Looking for "+ i);
        theSocket = new Socket(host, i);
        System.out.println("There is a server on port " + i);
      }
      catch (UnknownHostException e) {
        System.err.println(e);
        break;
      }
      catch (IOException e) {
        // must not be a server on this port
      }
    }