Reputation: 3956
I am using Java NIO and selectors to get instances of SocketChannel
s. I need to attach each instance of a SocketChannel that I get from the selector
to an encryption key that is unique to each SocketChannel
instance.
So I would like to keep a TreeMap
where the key is the SocketChannel
instance and then the value is the associated object to help with secure communications.
The problem is that the SocketChannel
object is not Comparable
(or whatever interface it needs to be to be a key) So I was using the SocketChannels hashCode()
method to get a hash for that socket channel.
I've heard that the SocketChannel's hash is not necessarily unique, and in a system with many many connections, I fear that a hash collision can happen, and that the wrong client could be exchanging data with the server. My small fix was managing all these containers that contained a SocketChannel
and a pointer to the next container, and to always check to see where the SocketChannel actually is the same object, but this leads to messy and ugly code.
Is there a better way to manage this situation?
Upvotes: 0
Views: 63