import java.net.*;
import java.io.IOException;

public class UDPPortScanner {

  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 {
        DatagramSocket ds = new DatagramSocket(port);
        ds.close();
      }
      catch (IOException e) {
        System.out.println("UDP Port " + port + " is occupied.");
      }
    }
  }
}