Forum: Ruby tcp multiple client / server... for each connection write

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bigmac T. (Guest)
on 2009-05-07 07:40
im putting together a server with multiple clients establishing a
connection... I need each client connected to retreave a string from the
server then the client's will process this string... string = gets, this
is how the server sends out the string but only the first client will
process this string...
I need the server to do something like this...

for each.connection.write(string)

does that make sense? im not sure how to accomplish this...

here is the source...

require 'socket'

server =

loop do
  Thread.start(server.accept) do |connection|
        print connection
      str  = gets
        connection.write(str)# this needs to stay alive or actave for
each connection
#---------------end of server-----------

require 'socket'

while true
sleep 1
streamSock = "", 1234 )
  var1,var2,var3 = streamSock.recv( 100 ).chomp.split

  if var1 == 'example'
    print var2+"\n"
          print var3+"\n"

  elsif var1 == 'test'
    print var2+"\n"
    print var3+"\n"

#-----------------end client-------------
Roger P. (Guest)
on 2009-05-07 10:36
> for each.connection.write(string)

I'd probably make the server less multi-threaded, something like

all_cons = []
all_cons << server.accept
all_cons << server.accept # now it has two connections.

Also look into the use of select.
This topic is locked and can not be replied to.