getting familiar with ruby tcp socket stuff
This commit is contained in:
parent
d83fcbe9c5
commit
0aaab6db1c
11
ruby-sockets/ping-pong-client.rb
Normal file
11
ruby-sockets/ping-pong-client.rb
Normal 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
|
15
ruby-sockets/ping-pong-server.rb
Normal file
15
ruby-sockets/ping-pong-server.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user