tags:

views:

189

answers:

1
A: 

Each connection to the server requires a separate accept call in order to be received. What's happening is that you're accepting the first, working with it, and then effectively terminating while leaving the socket in a listening state. This means connections will be opened, but not accepted, so they hang as you describe.

You might be better off using a more robust server framework. EventMachine (http://rubyeventmachine.com/) is a little tricky to learn, but is far more powerful than a roll your own solution.

Here's a quick fix that might help:

require "socket"
dts = TCPServer.new('localhost', 20000)
while (s = dts.accept)
  print(s, " is accepted\n")
  loopCount = 0;
  loop do
    Thread.start(s) do
    loopCount = loopCount + 1
      lineRcvd = s.recv(1024)
      if ( !lineRcvd.empty? )
        puts("#{loopCount} Received: #{lineRcvd}")
        s.write(Time.now)
      end
    end
  end
  s.close
  print(s, " is gone\n")
end

Now the accept call is wrapped in a loop so more than one connection can be processed.

tadman