Skip to main content
gamedev.net
🔒 Locked

Java NIO Session Tracking

Started by Dead6re 3 replies 2.7k views
Original Post
Dead6re
Dead6re
Hello,

I was looking at the new java IO for async IO and I can't figure out how to implement session tracking for clients (e.g. storing name, id, state [authenticated etc]).

If anyone has any good tutorials to hand that would be helpful too.

Thanks
hplus0603
hplus0603
For TCP, you still have a socket/file descriptor. Store the mapping between that object and the player state in a hash table.

For UDP, you still get the source IP and port of the packet. Store the mapping between that data and the player state in a hash table.
enum Bool { True, False, FileNotFound };
Dead6re
Dead6re
I assume the port is different for each client (the port that is assigned on accept for a server)
hplus0603
hplus0603
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().
enum Bool { True, False, FileNotFound };
Consty
Consty
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.