[JAVA] Tracking Individual Users

Started by
4 comments, last by hplus0603 11 years, 10 months ago
I've been trying to figure out how to create a multi user server for quite some time now, and have kept running into the same issue - how to track and manipulate individual users.
I found a code example, and turned it into a working Client / Server chat program. Here is my version of the code for reference, and to keep this post relatively short .... http://reviewerrick.blogspot.com/2012/06/working-java-socket-server-client.html

That code creates a new thread every time some one connects to the server, and they can make their own user name.

What I have been banging my head off a wall, is there is no way that I know of, to send data to only one of the connected clients.
All processed data is sent to all clients, and as far as I know of, there are no ways to access specific users ( by name ).
The main loop is an issue, also, due to the fact I can not process anything outside of the threads.

I'd love to create a session number for each client connected, and use that to individually manipulate the users.

I've been trolling Google for a very long time now on various subjects of networking, users, and the like .... lots of information on servers, no information on individual users.

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Advertisement
That code creates a new thread every time some one connects to the server[/quote]

That's a terrible idea! The FAQ for the forums talks about why.

Regarding your question: Each user has either a socket (for TCP) or a destination IP/port (for UDP.) Find the user by name (use a std::tr1::unordered_map or std::map or whatever) and send the message only to that user's endpoint.
enum Bool { True, False, FileNotFound };
I had the same problem as you. What I have done recently is to basically create a list of a Client class (make your own), inside that class can contains socket information, unique id assigned to that user, any other client-specific information. The client class can even make instances of other classes, or structs, that could contain more information (what level am I in a multi-player RPG?). Since you'll probably have a server game loop, get the server to check over each connected client in the Client list, extract every piece of networked buffered data available from each client and then depending on how you are making sense of messages process that message for that client and do what so ever accordingly.

Now that ive said that just look for the client in the Client list with the correct uniqueid that you want to send data to and send it to only that client.
Does the socket object contain the unique information about the user, or do I have to extract a specific part of it ?
If the whole socket object can be used, I could create a hashmap that has the generated session number, and the socket object of each user, which is referenced by the rest of the code when ever it does something relevant to the specific user.

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

What Xanather described is exactly, word for word, what Microsoft did in their .Net framework implementation of System.Net.TcpClient/UdpClient!!!
What Xanather described is exactly, word for word, what Microsoft did in their .Net framework[/quote]

Really? They use a linear list of all TcpClients? That's pretty inefficient compared to a hash table, or even better, just using an implicit reference where needed and no central look-up at all.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement