Why doesn't the following code throw a "java.net.BindException: Address already in use: JVM_Bind" exception?
import java.net.InetSocketAddress;
import java.net.ServerSocket;
public class Test
{
public static void main(String[] args) throws Exception
{
try (ServerSocket socket1 = new ServerSocket();
ServerSocket socket2 = new ServerSocket();
ServerSocket socket3 = new ServerSocket())
{
int port = 10000;
socket1.setReuseAddress(false);
socket1.bind(new InetSocketAddress("0.0.0.0", port));
socket2.setReuseAddress(false);
socket2.bind(new InetSocketAddress("127.0.0.1", port));
socket3.setReuseAddress(false);
socket3.bind(new InetSocketAddress("127.0.0.2", port));
Thread.sleep(Long.MAX_VALUE);
}
}
}
Running 'netstat' afterwards displays:
C:\Users\Administrator>netstat -a -n | findstr 10000 TCP 0.0.0.0:10000 0.0.0.0:0 LISTENING TCP 127.0.0.1:10000 0.0.0.0:0 LISTENING TCP 127.0.0.2:10000 0.0.0.0:0 LISTENING TCP [::]:10000 [::]:0 LISTENING
I am running this on Windows Server 2008 R2 (64-bit), and 'ipconfig /all' displays only one network adapter/interface (other network adapters are disabled). But, on some other machines, this program actually does throw the expected "java.net.BindException: Address already in use: JVM_Bind"!
What could be going on?