【问题标题】:LuaSocket (UDP) not receiving datagramsLuaSocket (UDP) 不接收数据报
【发布时间】:2015-01-23 08:12:09
【问题描述】:

我正在为我正在进行的项目试验 LuaSocket。我选择了 UDP 作为我的协议。

在网上寻找文档和教程,我尝试创建一个客户端-服务器对用于测试和学习。

根据我的阅读,以下代码应该可以工作。但是,似乎只有服务器工作正常。客户端发送消息,但不会收到服务器的回复。

感谢您提供的任何帮助。

服务器:

-- Server
#!/usr/bin/env lua5.1

local socket = require("socket")

udp = socket.udp()
udp:setsockname("*", 53474)
udp:settimeout(0)

while true do
    data, ip, port = udp:receivefrom()
    if data then
        print("Received: ", data, ip, port)
        udp:sendto(data, ip, port)
    end
    socket.sleep(0.01)
end

客户:

-- Client
#!/usr/bin/env lua5.1

local socket = require("socket")

udp = socket.udp()
udp:setpeername("127.0.0.1", 53474)
udp:settimeout(0)

udp:send("Data!")
data = udp:receive()
if data then
    print("Received: ", data)
end

【问题讨论】:

  • 删除或增加客户端超时。它对我有用。

标签: lua udp datagram luasocket


【解决方案1】:

你设置的超时值是0,每次都会导致客户端超时。

要修复它,给它一个正的超时值:

udp:settimeout(1)

或者设置为nil或者负值,这样就无限期阻塞了:

udp:settimeout()

udp:settimeout(-1)

【讨论】:

  • 谢谢!如此明显的解决方案,但我完全忽略了!
猜你喜欢
  • 2011-05-13
  • 2021-08-02
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 2020-08-29
  • 1970-01-01
相关资源
最近更新 更多