• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assign unique ID to clients

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic