Skip to content

Instantly share code, notes, and snippets.

@tagomoris
Created March 25, 2020 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tagomoris/f2fe8d1ed0321891166100ecce9182e5 to your computer and use it in GitHub Desktop.
Save tagomoris/f2fe8d1ed0321891166100ecce9182e5 to your computer and use it in GitHub Desktop.
require 'socket'
PORT = 8088
server = TCPServer.open(PORT)
children = []
puts "server.0 starting children"
9.times do |i|
num = i + 1 # 0 is for main
children << Thread.start(server) do |s|
puts "server.#{num} starting"
while sock = (s.accept rescue nil)
puts "server.#{num} " + sock.read
sock.close
end
end
end
puts "server.0 running"
running = true
Signal.trap(:INT) { running = false }
Signal.trap(:QUIT) { running = false }
Signal.trap(:TERM) { running = false }
while running
sleep 0.1
end
server.close
children.all? { |c| c.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment