Skip to content

@authorNari /a.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
# $ ruby a.rb
# $ nc localhost 8899
# $ pkill -TERM -f 'ruby this.rb'
require 'monitor'
require 'socket'
@monitor = Monitor.new
@threads = {}
Signal.trap("TERM") do
puts "trapped SIGTERM"
@monitor.synchronize do
puts "safe termination..."
@threads.values.each(&:join)
puts "...done"
exit
end
end
server = TCPServer.new(8899)
loop do
@monitor.synchronize do
th = Thread.new(server.accept) do |client|
client.puts "Hell"
sleep 20
client.puts "World..."
client.close
@threads.delete(Thread.current)
end
@threads[th] = th
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.