try { DatagramSocket ds = new DatagramSocket(); System.out.println("The DatagramSocket is on port " + ds.getLocalPort()); } catch (SocketException e) { }