Java tutorial
//package com.java2s; import java.net.DatagramSocket; import java.net.SocketException; import java.util.Random; public class Main { private static int port = 0; private static DatagramSocket datagramSocket; private static void BindPort() { Random random = new Random(); port = random.nextInt(65535); try { datagramSocket = new DatagramSocket(port); } catch (SocketException e) { // TODO Auto-generated catch block BindPort(); } } }