Atom Feed SITE FEED   ADD TO GOOGLE READER

Bind several times to the same port in Mac OS X

My PowerBook lets me bind to the same port twice using NIO. The following code throws an IOException under Windows and Linux, but not under Mac OS X:

    public void bindTwice(int port) throws IOException {

InetSocketAddress listenAddress = new InetSocketAddress(port);

ServerSocketChannel firstChannel = ServerSocketChannel.open();
ServerSocket firstSocket = firstChannel.socket();
firstSocket.bind(listenAddress);

ServerSocketChannel secondChannel = ServerSocketChannel.open();
ServerSocket secondSocket = secondChannel.socket();
secondSocket.bind(listenAddress);
}


It wouldn't be too big a deal but I want my application to fail if it can't bind! One horrible problem that has come up in my tests is this:
1. Bind to port X as server.
2. Connect to self on port X as client.
Now the client has fully connected but server has not received anything!

Yuck. And Google doesn't tell me anything. Yuck yuck.