// locate which of the first 1024 ports seem to be hosting TCP servers
// host is read in from the command line or defaults to localhost
public static void main(String[] args) {
Socket theSocket;
String host = "localhost";
if (args.length > 0)
{ host = args[0]; }
for (int i = 0; i < 1024; i++)
{
try {
theSocket = new Socket(host, i);
System.out.println("There is a server on port " + i + " of " + host);
}
catch (UnknownHostException e)
{ System.err.println(e); break;}
catch (IOException e)
{ // must not be a server on this port
}
}
}
}