-
-
Save sh7d/489e7419de6716433d68c38f887762c6 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' | |
READ_TOUT = 2 | |
SERV_ADDR = '127.0.0.1' | |
SERV_PORT = 2000 | |
Thread.abort_on_exception = true | |
c = TCPSocket.new(SERV_ADDR, SERV_PORT) | |
cth = Thread.new do | |
begin | |
while line = c.gets | |
puts line | |
Thread.current[:last] = Time.now.to_i | |
end | |
rescue | |
Thread.exit | |
end | |
end | |
sleep 0.5 until cth[:last] | |
loop do | |
res = Time.now.to_i - cth[:last] | |
if (res > READ_TEOUT) | |
c.close | |
break | |
end | |
sleep 1 | |
end | |
cth.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment