import java.net.*;
import java.io.IOException;
public class LocalPortScanner {
public static void main(String[] args) {
// first test to see whether or not we can bind to ports
// below 1024
boolean rootaccess = false;
for (int port = 1; port < 1024; port += 50) {
try {
ServerSocket ss = new ServerSocket(port);
// if successful
rootaccess = true;
ss.close();
break;
}
catch (IOException e) {
}
}
int startport = 1;
if (!rootaccess) startport = 1024;
int stopport = 65535;
for (int port = startport; port <= stopport; port++) {
try {
ServerSocket ss = new ServerSocket(port);
ss.close();
}
catch (IOException e) {
System.out.println("Port " + port + " is occupied.");
}
}
}
}