3
我有一個獨立的lua腳本,它使用lua套接字通過TCP IP連接到服務器。它使用接收呼叫從該服務器接收數據。但是,當我嘗試使用Ctrl + C停止它時,會發生以下兩種情況之一:Lua套接字無法通過Ctrl + C正確停止
- 如果當前沒有流量並且接收正在等待,則Ctrl + C將不起作用。該計劃將繼續運行,並將不得不被終止。
- 如果有流量,該項目將與下面的打印輸出,並與插座仍處於打開狀態,並與服務器不接受另一個連接退出:
lua: luaSocketTest.lua:15: interrupted!
stack traceback:
[C]: in function 'receive'
luaSocketTest.lua:15: in function 'doWork'
luaSocketTest.lua:22: in main chunk
[C]: ?
我試着用PCALL解決第二個方案中,沒有成功。 pcall不返回,該進程仍然會拋出錯誤。
樣品我的計劃是如下:
local socket = require ("socket")
local ip = "localhost"
local port = 5003
function doWork()
print ("Starting socket: "..ip..":"..port)
client = assert(socket.connect(ip, port))
print ("Socket Accepted")
client:send("TEST TEST")
while 1 do
local byte, err = client:receive (1)
if not err then
print (byte)
end
end
end
while 1 do
local status = pcall(doWork())
print ("EXITED PCALL WITH STATUS: "..tostring(status))
if not status then client:close() end
end
您正在使用哪種操作系統? –
我認爲這可以通過設置客戶端超時爲0,並定期檢查新的數據到達套接字來避免。在睡眠期間,應該可以使用ctrl-c。 – jpjacobs
洛倫佐,使用Linux – elanamig