Skip to content

Instantly share code, notes, and snippets.

@qichunren
Created November 18, 2011 02:29
Show Gist options
  • Save qichunren/1375391 to your computer and use it in GitHub Desktop.
Save qichunren/1375391 to your computer and use it in GitHub Desktop.
ruby udp
require 'socket'
socket = UDPSocket.new
socket.send("I am ok.", 0, 'localhost', 1234)
text, sender = socket.recvfrom(16)
socket.close
# encoding: utf-8
require 'socket'
server = UDPSocket.new
host, port = "127.0.0.1", 1234
server.bind(host, port)
while true
text, sender = server.recvfrom(16)
remote_host = sender[3]
STDOUT.puts "#{remote_host}:" + text
server.send("received.", 0, host, port)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment