fork a number of listening processes to increase the maximum number of connections : fork « Threads « Ruby






fork a number of listening processes to increase the maximum number of connections


require 'socket'

server = TCPServer.new(1234)

5.times do
  fork do
    while connection = server.accept
      while line = connection.gets
        break if line =~ /quit/
        puts line
        connection.puts "Received!"
      end

      connection.puts "Closing the connection. Bye!"
      connection.close
    end
  end
end

 








Related examples in the same category

1.fork is a method provided by the Kernel module that creates a fork of the current process.
2.Fork a child process
3.Is it a parent process or a child process
4.fork with block
5.Environment in Child thread