From: Kirk Haines Date: 2006-06-07T01:44:49+09:00 Subject: Re: Multiple threads on single port On Tuesday 06 June 2006 8:55 am, Stuart Brand wrote: > require 'socket' > port = (ARGV[0] || 80).to_i > server = TCPServer.new('localhost', port) > threads = [] > > > 10.times do |i| > threads[i] = Thread.new { > while (session = server.accept) > puts "Request: #{session.gets}" > session.print "HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n" > session.print "

#{Time.now}

\r\n" > session.close > end > } > end Don't do this. Just have a thread of execution that is in a while loop doing the accepts on the socket, and have it spawn threads to handle the connections. def handle_connection(session) puts "Request: #{session.gets}" session.print "HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n" session.print "

#{Time.now}

\r\n" session.close end while (session = server.accept) Thread.new {handle_connection(session)} end Kirk Haines