Month: August 2014

Simple closures for TCP communications

Posted on Updated on

For connect to a TCP server :

th=GClient.run_one_shot("localhost",ARGV[0].to_i) do |socket|
  socket.write "GET / HTTP/1.1\r\n\r\n"
  puts "waiting response...\n\n<<<"
  timeout(4) { puts socket.gets while !socket.closed? }
end

For keeping a connection to server :

th=GClient.run_continous("localhost",2233,10) do |socket|
  socket.write "a"*100
  puts "waiting response...\n\n"
  timeout(22) { puts socket.gets while true}
end
th.join

 

See gem minitcp  ( https://github.com/glurp/minitcp )