Sign in to follow this  
RLS0812

[JAVA] Tracking Individual Users

Recommended Posts

RLS0812    3043
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.

Share this post


Link to post
Share on other sites
hplus0603    11356
[quote]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.

Share this post


Link to post
Share on other sites
Xanather    782
I had the same problem as you. What I have done recently is to basically create a list of a [b]Client[/b] 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 [b]Client[/b] 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 [b]Client[/b] list with the correct uniqueid that you want to send data to and send it to only that client. Edited by Xanather

Share this post


Link to post
Share on other sites
RLS0812    3043
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. Edited by Shippou

Share this post


Link to post
Share on other sites
?????????    199
What Xanather described is exactly, word for word, what Microsoft did in their .Net framework implementation of System.Net.TcpClient/UdpClient!!!

Share this post


Link to post
Share on other sites
hplus0603    11356
[quote]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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this