Created
November 18, 2011 02:29
-
-
Save qichunren/1375391 to your computer and use it in GitHub Desktop.
ruby udp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
socket = UDPSocket.new | |
socket.send("I am ok.", 0, 'localhost', 1234) | |
text, sender = socket.recvfrom(16) | |
socket.close |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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