Skip to content

Instantly share code, notes, and snippets.

@sawanoboly
Created May 15, 2012 06:20
Show Gist options
  • Save sawanoboly/2699560 to your computer and use it in GitHub Desktop.
Save sawanoboly/2699560 to your computer and use it in GitHub Desktop.
TCP Simple push2
require "socket"
require "time"
require "date"
gs = TCPServer.open(0, 20000)
socks = [gs]
addr = gs.addr
addr.shift
printf("server is on %s\n", addr.join(":"))
while true
Thread.start(gs.accept) do |s|
print(s, " is accepted\n")
s.write("Please send integer x. Server will return response after x seconds.\n\n")
while s.gets
ii = $_.to_i
if ii > 0
tts = Time.now
ttd = tts + ii
s.write("See you after #{ii} seconds!\n")
s.write(" Now: #{tts}\n")
s.write("Until: #{ttd}\n\n")
sleep ii
tts = Time.now
s.write("Have you been waiting #{ii} seconds?\n")
s.write(" Now: #{tts}\n\n")
s.write("Please send integer x. Server will return response after x seconds.\n\n")
end
end
print(s, " is gone")
s.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment