Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,10 +17,12 @@
package org.springframework.util;

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.Random;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.net.ServerSocketFactory;

/**
Expand All @@ -34,6 +36,7 @@
* @author Ben Hale
* @author Arjen Poutsma
* @author Gunnar Hillert
* @author Gary Russell
* @since 4.0
*/
public class SocketUtils {
Expand Down Expand Up @@ -196,7 +199,8 @@ private static enum SocketType {
@Override
protected boolean isPortAvailable(int port) {
try {
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port);
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(
port, 1, InetAddress.getByName("localhost"));
serverSocket.close();
return true;
}
Expand All @@ -210,7 +214,7 @@ protected boolean isPortAvailable(int port) {
@Override
protected boolean isPortAvailable(int port) {
try {
DatagramSocket socket = new DatagramSocket(port);
DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"));
socket.close();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,13 @@

package org.springframework.util;

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.SortedSet;

import javax.net.ServerSocketFactory;

import org.junit.Test;

import static org.junit.Assert.*;
Expand All @@ -27,6 +32,7 @@
* Unit tests for {@link SocketUtils}.
*
* @author Sam Brannen
* @author Gary Russell
*/
public class SocketUtilsTests {

Expand Down Expand Up @@ -60,6 +66,18 @@ public void findAvailableTcpPort() {
assertPortInRange(port, PORT_RANGE_MIN, PORT_RANGE_MAX);
}

@Test(expected = IllegalStateException.class)
public void findAvailableTcpPortLoopback() throws Exception {
int port = SocketUtils.findAvailableTcpPort();
ServerSocket socket = ServerSocketFactory.getDefault().createServerSocket(port, 1, InetAddress.getByName("localhost"));
try {
SocketUtils.findAvailableTcpPort(port, port+1); // will actually only look for port - random.nextInt(1) always returns 0
}
finally {
socket.close();
}
}

@Test
public void findAvailableTcpPortWithMin() {
int port = SocketUtils.findAvailableTcpPort(50000);
Expand Down Expand Up @@ -127,6 +145,18 @@ public void findAvailableUdpPort() {
assertPortInRange(port, PORT_RANGE_MIN, PORT_RANGE_MAX);
}

@Test(expected = IllegalStateException.class)
public void findAvailableUdpPortLoopback() throws Exception {
int port = SocketUtils.findAvailableUdpPort();
DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost"));
try {
SocketUtils.findAvailableUdpPort(port, port+1); // will actually only look for port - random.nextInt(1) always returns 0
}
finally {
socket.close();
}
}

@Test
public void findAvailableUdpPortWithMin() {
int port = SocketUtils.findAvailableUdpPort(50000);
Expand Down