当前位置:   article > 正文

lua udp mysql,LuaSocket(UDP)无法接收数据报

lua udp socket read timed out, client

I'm experimenting with LuaSocket for a project I'm working on. I've chosen UDP as my protocol.

Looking for docs and tutorials online, I've attempted to create a client-server pair for testing and learning.

According to what I've read, the following code should work. However, only the server seems to be working properly. The client sends a message, but it will not receive the reply from the server.

Thank you for any help anyone can provide.

Server:

-- 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:

-- 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

解决方案

The timeout value you set is 0, which causes the client timeout every time.

To fix it, give it a positive timeout value:

udp:settimeout(1)

Or set it to nil or a negative value, so it blocks indefinitely:

udp:settimeout()

or

udp:settimeout(-1)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/940465
推荐阅读
相关标签
  

闽ICP备14008679号