Created
March 25, 2020 07:07
-
-
Save tagomoris/f2fe8d1ed0321891166100ecce9182e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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