Here is a simple port scanner that scan all ports:
import java.net.*;
import java.io.*;
public class ScanPort {
public static void main(String args[]) {
for (int portNumber = 0; portNumber < 65536; portNumber++) {
try {
System.out.println("checking for port " + portNumber + " availability");
/*create a ServerSocket */
ServerSocket serverSocket = new ServerSocket(portNumber);
/*Available portNumbers*/
System.out.println("checking for port " + portNumber + " availability, succeeded");
/*if port number is occupied an IOException will be throwen*/
catch (IOException x) {
System.out.println("ERROR MESSAGE: checking for port " + portNumber +
" availability, failed");
}
}
}
}
see also:
http://java.sun.com/j2se/1.5.0/docs/api/java/net/ServerSocket.html