import java.net.*; public class getLocalPortExample { public static void main(String[] args) { try { DatagramSocket ds = new DatagramSocket(); System.out.println("The DatagramSocket is on port " + ds.getLocalPort()); } catch (SocketException e) { } } }