/** * Find an available UDP port randomly selected from the range * [{@code minPort}, {@value #PORT_RANGE_MAX}]. * @param minPort the minimum port number * @return an available UDP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableUdpPort(int minPort) { return findAvailableUdpPort(minPort, PORT_RANGE_MAX); }
/** * Find an available UDP port randomly selected from the range * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. * @return an available UDP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableUdpPort() { return findAvailableUdpPort(PORT_RANGE_MIN); }
/** * Find an available UDP port randomly selected from the range * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. * @return an available UDP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableUdpPort() { return findAvailableUdpPort(PORT_RANGE_MIN); }
/** * Find an available UDP port randomly selected from the range * [{@code minPort}, {@value #PORT_RANGE_MAX}]. * @param minPort the minimum port number * @return an available UDP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableUdpPort(int minPort) { return findAvailableUdpPort(minPort, PORT_RANGE_MAX); }
@Test public void findAvailableUdpPortWithNegativeMinPort() { exception.expect(IllegalArgumentException.class); SocketUtils.findAvailableUdpPort(-500); }
@Test public void findAvailableUdpPortWithZeroMinPort() { exception.expect(IllegalArgumentException.class); SocketUtils.findAvailableUdpPort(0); }
@Test public void findAvailableUdpPort() { int port = SocketUtils.findAvailableUdpPort(); assertPortInRange(port, PORT_RANGE_MIN, PORT_RANGE_MAX); }
@Test public void findAvailableUdpPortWithMin() { int port = SocketUtils.findAvailableUdpPort(50000); assertPortInRange(port, 50000, PORT_RANGE_MAX); }
@Test public void findAvailableUdpPortInRange() { int minPort = 20000; int maxPort = minPort + 1000; int port = SocketUtils.findAvailableUdpPort(minPort, maxPort); assertPortInRange(port, minPort, maxPort); }
@Test public void findAvailableUdpPortWhenPortOnLoopbackInterfaceIsNotAvailable() throws Exception { int port = SocketUtils.findAvailableUdpPort(); DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost")); try { exception.expect(IllegalStateException.class); exception.expectMessage(startsWith("Could not find an available UDP port")); exception.expectMessage(endsWith("after 1 attempts")); // will only look for the exact port SocketUtils.findAvailableUdpPort(port, port); } finally { socket.close(); } }
/** * Find an available UDP port randomly selected from the range * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. * @return an available UDP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableUdpPort() { return findAvailableUdpPort(PORT_RANGE_MIN); }
/** * Find an available UDP port randomly selected from the range * [{@code minPort}, {@value #PORT_RANGE_MAX}]. * @param minPort the minimum port number * @return an available UDP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableUdpPort(int minPort) { return findAvailableUdpPort(minPort, PORT_RANGE_MAX); }
/** * Find an available UDP port randomly selected from the range * [{@value #PORT_RANGE_MIN}, {@value #PORT_RANGE_MAX}]. * @return an available UDP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableUdpPort() { return findAvailableUdpPort(PORT_RANGE_MIN); }
/** * Find an available UDP port randomly selected from the range * [{@code minPort}, {@value #PORT_RANGE_MAX}]. * @param minPort the minimum port number * @return an available UDP port number * @throws IllegalStateException if no available port could be found */ public static int findAvailableUdpPort(int minPort) { return findAvailableUdpPort(minPort, PORT_RANGE_MAX); }
private static int findAvailableUDPPort() { return SocketUtils.findAvailableUdpPort(minPort, maxPort); }
@Autowired public AbstractUdpReceiverPublisher(OdeProperties odeProps, int port, int bufferSize) { this.odeProperties = odeProps; this.port = port; this.bufferSize = bufferSize; try { socket = new DatagramSocket(this.port); logger.info("Created UDP socket bound to port {}", this.port); } catch (SocketException e) { logger.error("Error creating socket with port " + this.port + " Will use the next availabel port.", e); this.port = SocketUtils.findAvailableUdpPort(); logger.info("Using alternate port {}", this.port); try { socket = new DatagramSocket(this.port); logger.info("Created UDP socket bound to port {}", this.port); } catch (SocketException e2) { logger.error("Error creating socket with port " + this.port, e); } } }