LuaSocket server:accept() 超时(TCP)
LuaSocket server:accept() timeout (TCP)
问题
关注 LuaSocket
Introduction 我设法获得了服务器 运行。我还设法从客户端连接。但是,我注意到服务器脚本会冻结,直到 server:accept()
获得连接。
研究
LuaSocket
Reference 指定:
Use the settimeout method or accept might block until another client shows up.
这甚至包含在示例代码中。但是,client:settimeout(10)
在 local client = server:accept()
之后调用,因此脚本在到达此点之前被阻止。
我读到这可以通过多线程解决,但这似乎有点夸张。
问题
- 如何使服务器脚本停止等待连接并继续前进?
- 如何防止
client:receive()
(服务器端)和 tcp:receive()
(客户端)出现类似问题(或者 client:settimeout(10)
负责)?
代码
服务器(来自LuaSocket
Introduction)
-- load namespace
local socket = require("socket")
-- create a TCP socket and bind it to the local host, at any port
local server = assert(socket.bind("*", 0))
-- find out which port the OS chose for us
local ip, port = server:getsockname()
-- print a message informing what's up
print("Please telnet to localhost on port " .. port)
print("After connecting, you have 10s to enter a line to be echoed")
-- loop forever waiting for clients
while 1 do
-- wait for a connection from any client
local client = server:accept()
-- make sure we don't block waiting for this client's line
client:settimeout(10)
-- receive the line
local line, err = client:receive()
-- if there was no error, send it back to the client
if not err then client:send(line .. "\n") end
-- done with client, close the object
client:close()
end
客户(关注 this answer)
local host, port = "127.0.0.1", 100
local socket = require("socket")
local tcp = assert(socket.tcp())
tcp:connect(host, port);
--note the newline below
tcp:send("hello world\n");
while true do
local s, status, partial = tcp:receive()
print(s or partial)
if status == "closed" then break end
end
tcp:close()
您应该可以在调用 server:accept()
之前使用 server:timeout():
server:settimeout(2)
local client, err = server:accept()
print(client, err)
如果 2 秒内没有请求,这会为我打印 nil timeout
。
问题
关注 LuaSocket
Introduction 我设法获得了服务器 运行。我还设法从客户端连接。但是,我注意到服务器脚本会冻结,直到 server:accept()
获得连接。
研究
LuaSocket
Reference 指定:
Use the settimeout method or accept might block until another client shows up.
这甚至包含在示例代码中。但是,client:settimeout(10)
在 local client = server:accept()
之后调用,因此脚本在到达此点之前被阻止。
我读到这可以通过多线程解决,但这似乎有点夸张。
问题
- 如何使服务器脚本停止等待连接并继续前进?
- 如何防止
client:receive()
(服务器端)和tcp:receive()
(客户端)出现类似问题(或者client:settimeout(10)
负责)?
代码
服务器(来自LuaSocket
Introduction)
-- load namespace
local socket = require("socket")
-- create a TCP socket and bind it to the local host, at any port
local server = assert(socket.bind("*", 0))
-- find out which port the OS chose for us
local ip, port = server:getsockname()
-- print a message informing what's up
print("Please telnet to localhost on port " .. port)
print("After connecting, you have 10s to enter a line to be echoed")
-- loop forever waiting for clients
while 1 do
-- wait for a connection from any client
local client = server:accept()
-- make sure we don't block waiting for this client's line
client:settimeout(10)
-- receive the line
local line, err = client:receive()
-- if there was no error, send it back to the client
if not err then client:send(line .. "\n") end
-- done with client, close the object
client:close()
end
客户(关注 this answer)
local host, port = "127.0.0.1", 100
local socket = require("socket")
local tcp = assert(socket.tcp())
tcp:connect(host, port);
--note the newline below
tcp:send("hello world\n");
while true do
local s, status, partial = tcp:receive()
print(s or partial)
if status == "closed" then break end
end
tcp:close()
您应该可以在调用 server:accept()
之前使用 server:timeout():
server:settimeout(2)
local client, err = server:accept()
print(client, err)
如果 2 秒内没有请求,这会为我打印 nil timeout
。