簡體   English   中英

LuaSocket(UDP)無法接收數據報

[英]LuaSocket (UDP) not receiving datagrams

我正在與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

您設置的超時值為0 ,這每次都會導致客戶端超時。

要解決此問題,請給它一個正的超時值:

udp:settimeout(1)

或將其設置為nil或負值,因此它會無限期阻止:

udp:settimeout()

要么

udp:settimeout(-1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM