# TCPServer Muliple Clients by Name or Alias

**URL:** <https://www.ruby-forum.com/t/tcpserver-muliple-clients-by-name-or-alias/191879>\
**Category:** Ruby\
**Created:** [July 20, 2010, 5:34am UTC](https://www.ruby-forum.com/t/tcpserver-muliple-clients-by-name-or-alias/191879 "2010-07-20T05:34:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![grauenwolf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e8c25b/32.png) [@grauenwolf](https://www.ruby-forum.com/u/grauenwolf)\
**Post date:** [July 20, 2010, 5:34am UTC](https://www.ruby-forum.com/t/tcpserver-muliple-clients-by-name-or-alias/191879/1 "2010-07-20T05:34:56Z")

</div>

Dear List,

I have read over the TCPServer docs and I have been able to implement a  
simple server that supports multiple clients and will echo input back to  
all users. What I cannot seem to find docs on or grasp, is how to  
reference each individual client by a name.

What I wish to accomplish is when a new client connects, they enter  
their name/alias so that other users can send messages to individual  
users by their name. Also, when they send a line of text to the server,  
everyone else sees something like the following.

nocjallen says Hello World!

I’m not sure how to go about doing this. Currently my script creates a  
new thread for each connection adds each connection to an array and then  
loops through it and sends what one client has said, to each other  
connection in the array.

Any helpful input, examples or docs would be greatly appreciated.

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![grauenwolf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e8c25b/32.png) [@grauenwolf](https://www.ruby-forum.com/u/grauenwolf)\
**Post date:** [July 20, 2010, 1:46pm UTC](https://www.ruby-forum.com/t/tcpserver-muliple-clients-by-name-or-alias/191879/2 "2010-07-20T13:46:42Z")

</div>

2010/7/20 Jonathan A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> everyone else sees something like the following.
> 
> nocjallen says Hello World!
> 
> I’m not sure how to go about doing this. Currently my script creates a  
> new thread for each connection adds each connection to an array and then  
> loops through it and sends what one client has said, to each other  
> connection in the array.
> 
> Any helpful input, examples or docs would be greatly appreciated.

You need a Hash to store your connections in. Then you must create a  
protocol by which every client authenticates itself and sends its  
name. You’ll likely want to store more than the raw socket in the  
Hash, e.g. something like

ClientInfo = Struct.new :socket, :name

Note that you must synchronize access to the centralized Hash which  
maps names to ClientInfo instances.

Kind regards

robert
