getting familiar with ruby tcp socket stuff

This commit is contained in:
Dan Buch 2012-06-15 09:49:35 -04:00
parent d83fcbe9c5
commit 0aaab6db1c
2 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,11 @@
require 'socket'
loop do
TCPSocket.open('127.0.0.1', 24000) do |sock|
sock.send("PING\n", 0)
pong = sock.recv(100)
puts "client: RECV PONG"
sock.close
end
sleep 0.5
end

View File

@ -0,0 +1,15 @@
require 'socket'
server = TCPServer.new('0.0.0.0', 24000)
loop do
Thread.start(server.accept) do |client|
if (ping = client.gets) =~ /PING/
puts 'server: RECV PING'
client.puts("PONG\n")
else
puts "server: RECV #{ping}"
client.puts("NOPE\n")
end
client.close
end
end