Java Sockets

java.net.Socket

import java.net.*;
import java.io.*;


public class LowPortScanner {

  public static void main(String[] args) {

    //A hostname
    String host = "localhost";

    if (args.length > 0) {
      host = args[0];
    }
    for (int port = 1; port < 1024; port++) {
      try {
        //Open a socket.
        Socket s = new Socket(host, port);
        System.out.println("There is a server on port " + port + " of " + host);
      }
      catch (UnknownHostException e) {
        System.err.println(e);
        break;
      }
      catch (IOException e) {
        // must not be a server on this port
      }
    } // end for
  
  }  // end main
  
}  // end PortScanner


José M. Vidal .

5 of 9