Ruby socket server thread question: how to send to all clients?

Posted by Paul on Stack Overflow See other posts from Stack Overflow or by Paul
Published on 2010-04-26T12:53:27Z Indexed on 2010/04/26 14:23 UTC
Read the original article Hit count: 362

Filed under:

I'm making a TCP socket server(ruby). A thread is created for each connected client. At some point I try to send data to all connected clients. The thread aborts on exception while trying.(ruby 1.8.7)

require 'socket'
# I test it home right now
server = TCPServer.new('localhost', 12345);
while(session = server.accept)
 #Here is the thread being created
 Thread.new(session) do |s|
  while(msg = s.gets)
   #Here is the part that causes the error
   Thread.list.each { |aThread|
    if aThread != Thread.current
     #So what I want it to do is to echo the message from one client to all others
     #But for some reason it doesn't, and aborts on the following string
     aThread.print "#{msg}\0"
    end
   }
  end
 end
Thread.abort_on_exception = true
end

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about ruby