Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Assign unique ID to clients  RSS feed

 
Manasa Rao
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a chat server and want to assign a unique id to each client that connects to the server. Then, display that id while displaying the text messages.
Ex:
Client1>Hi
Client2>Hello
Can someone please suggest how can I do that? I have three major classes: Client.java (which takes care of initiating a connection to the server), ServerListener.java (which keeps listening to the coming requests), and ServerHandler.java (which takes care of processing the requests).
Thanks in advance,
Manasa
 
Paul Frey
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many ways to implement unique client identification. If there is only one chat server and the clients have to be unique within that server, then you could keep a list of all active chat clients (probably in the ServerListener.java). Then during the handshake between the server and client, you send the assigned ID to the client (which would include the index of that client in the list of clients).
Another method would be to use either java.rmi.dgc.VMID or java.rmi.server.UID to come up with a unique name you could send to the client during the initial handshake.
Once the initial handshake is done, all subsequent messages from the client would transmit the ID they were assigned. The advantage to this method is no housekeeping is necessary to purge a list of clients and the ID would be unique across chat servers.
Hope this helped.
Paul Frey
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!