Port Scanner

You cannot just connect to any port on any host. The remote host must actually be listening for connections on that port. You can use the constructors to determine which ports on a host are listening for connections.

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


public class PortScanner {

  public static void main(String[] args) {

    for (int i = 0; i < args.length; i++) {
      try {
        InetAddress ia = InetAddress.getByName(args[i]);
        scan(ia);
      }
      catch (UnknownHostException ex) {
        System.err.println(args[i] + " is not a valid host name.");
      }
    }

  }

  public static void scan(InetAddress remote) {

    // Do I need to synchronize remote?
    // What happens if someone changes it while this method
    // is running?

    String hostname = remote.getHostName();
    for (int port = 0; port < 65536; port++) {
      try {
        Socket s = new Socket(remote, port); 
        System.out.println("A server is listening on port " + port
         + " of " + hostname);
        s.close();
      }
      catch (IOException ex) {
        // The remote host is not listening on this port
      }
    }

  }

  public static void scan(String remote) throws UnknownHostException {

    // Why throw the UnknownHostException? Why not catch it like I did
    // in the main() method?
    InetAddress ia = InetAddress.getByName(remote);
    scan(ia);

  }

}

Warning: Pointing PortScanner at a machine you do not own would generally be considered a hostile act by the owner of the machine.


Previous | Next | Top | Cafe au Lait

Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified April 17, 1997