UDP套接字编程帮助

我正在寻找有关UDP套接字编程的良好教程。我已经尝试过搜索引擎,但是没有找到任何有用的信息,因此我来这里发布帖子。我的基本目标是通过UDP端口在远程机器上读写数据,如果有人能提供一些帮助/教程链接,我将不胜感激。

我的最终目标是在Ruby和/或Lua中编写代码,但是任何语言的教程都可以帮助理解逻辑。

谢谢!

原文链接 https://stackoverflow.com/questions/6599184

点赞
stackoverflow用户32453
stackoverflow用户32453

Ruby UDP Socket Example

Here is a simple example of client-server socket programming using Ruby’s UDPSocket:

require 'socket'

# server

socket = UDPSocket.new
socket.bind(nil, 1234)
5.times do
  text, sender = socket.recvfrom(16)
  puts text
  socket.send(Time.now.to_s, 0, sender[3], sender[1])
end

# client

socket = UDPSocket.new
socket.send("hello", 0, 'localhost', 1234)
text, sender = socket.recvfrom(100)
puts text

The server code binds the socket to all interfaces using nil as the host and 1234 as the port number. It then listens for incoming messages and responds with the current time. The client code sends a message of “hello” to localhost on port 1234 and waits for a response.

That’s it — over and out!

2011-07-06 22:31:39