TCPServer Muliple Clients by Name or Alias

J

Jonathan Allen

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.
 
R

Robert Klemme

2010/7/20 Jonathan Allen said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top