tags:

views:

82

answers:

1

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?

A: 

The following PDF discusses socket programming in ruby in detail. It includes asynchronous handling and goes through a functioning demonstration of a small chat program.

IBM guide to ruby socket programming

Beanish