lockend 发表于 2011-03-24 09:46

Ruby 简单的 UDP 程序示例

require 'socket'

host = 'localhost'
port = 1234

s = UDPSocket.new
s.bind(nil, port)
s.send("1", 0, host, port)

5.times do
text, sender = s.recvfrom(16)
remote_host = sender

puts "#{remote_host} sent #{text}"

response = (text.to_i * 2).to_s
puts "We will respond with #{response}"

s.send(response, 0, host, port)
end
页: [1]
查看完整版本: Ruby 简单的 UDP 程序示例