Original Post
🔒 Locked
Java NIO Session Tracking
Advertisement
That is a patently false assumption.
For TCP, the session is identified by the tuple (client ip, client port, server ip, server port). The server port is always the port you are listening on. The client TCP stack will assign a port randomly to the connection if you haven't already used bind() on a socket when calling connect(); that does not affect the server.
For UDP, there is no "session" per se, but most servers tend to use the remote ip and remote port as the "identifier" and assume a timeout error if some period of time passes without traffic from that source. UDP traffic is also always routed to the given port and ip of the server. Again, UDP sockets will have a port randomly assigned by the implementation if and only if the socket hasn't already been bound to a port before the first send()/sendto().
For TCP, the session is identified by the tuple (client ip, client port, server ip, server port). The server port is always the port you are listening on. The client TCP stack will assign a port randomly to the connection if you haven't already used bind() on a socket when calling connect(); that does not affect the server.
For UDP, there is no "session" per se, but most servers tend to use the remote ip and remote port as the "identifier" and assume a timeout error if some period of time passes without traffic from that source. UDP traffic is also always routed to the given port and ip of the server. Again, UDP sockets will have a port randomly assigned by the implementation if and only if the socket hasn't already been bound to a port before the first send()/sendto().
I think he's asking more generally how you implement such a construct in the context of the Java NIO abstraction. Simply put, when you register your SocketChannel object with a Selector object, it returns a SelectionKey object. Use the attach() method to attach whatever player object containing all the game specific crap in it. Then when you get a list of selection keys that need to be read, you can access key.attachment() to get the player associated with that SocketChannel. This is what the attach() and attachment() methods were created for. Hope it helps.
See: http://download.oracle.com/javase/6/docs/api/java/nio/channels/SelectionKey.html
See: http://download.oracle.com/javase/6/docs/api/java/nio/channels/SelectionKey.html
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement