2

I am trying to create TCP client in lua

local host, port = HOST, PORT
local socket = require("socket")
client = socket.tcp();
client:connect(host, port);
client:send("Hello User");

this works fine but when i add

while true do
    local s, status, partial = client:receive()
    print(s or partial)
    if status == "closed" then break end
end

to read data from socket it block total execution of code.

4

1 回答 1

1

默认情况下,所有 luasocket I/O 操作都是阻塞的。您需要使用socket.settimeout(0)( settimeout ) 禁用阻塞。然后,您可以检查作为状态返回的“超时”值并采取相应措施。

根据数据的发送方式,这个答案也可能是相关的。

于 2013-08-13T22:05:53.587 回答