When the AbstractIoAcceptor binds a socket, it adds an entry to the "boundAddresses" set to keep track of that socket.
In my case, I am passing a SocketAddress initialized with a specific port, and "null" for host (I want to listen on that port on all interfaces on the the local machine, i.e 0.0.0.0:9999). This InetSocketAddress instance has an Inet4Address in it when created.
When the Acceptor is given this v4 SocketAddress to bind, it creates a ServerSocketChannel, and then calls ServerSocketChannel.socket().getLocalSocketAddress() to retrieve a SocketAddress. This is the value that is then put in the "boundAddresses" collection.
The problem is that the ServerSocketChannel.socket().getLocalSocketAddress() returns a SocketAddress with an Inet6Address value in it (0.0.0.0.0.0.0.0:9999), even though the SocketAcceptor.bind() was given a v4 address. So when I try to unbind that socket (using the same v4 address I used to do the bind() call), it fails because the "boundAddresses" set has the v6 version in it, and "Inet6Address.equals(Inet4Address) is false".
1. I call NioSocketAcceptor.bind() on a v4 SocketAddress
2. A ServerSocketChannel is created for that v4 address.
3. The ServerSocketChannel.socket().getLocalSocketAddress() is called, which returns a v6 SocketAddress (which is the equivalent of the v4 SocketAddress given)
4. NioSocketAcceptor puts the v6 address in its "boundAddresses" list.
5. When I ask to unbind the same v4 address, it fails because the "boundAddresses" list only contains a v6 address.
1. Make the "boundAddresses" field a Map<SocketAddress,SocketAddress>. The keys should be the SocketAddress parameter passed to the bind() call, the values should be the ServerSocketChannel.socket().getLocalSocketAddress().
2. When unbind(a) is called, check "boundAddresses.hasKey(a)", and if true, then unbind "boundAddresses.get(a)".
This makes it consistent for the client; they can use the same instance of SocketAddress to bind and unbind reliably.
This will match Mina v1.x version, where the SocketAddress passed to the bind() method could be used to also unbind.