LuaSocket服务器:accept()超时(TCP)

编程入门 行业动态 更新时间:2024-10-19 14:30:28
本文介绍了LuaSocket服务器:accept()超时(TCP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题

跟随LuaSocket 引言我设法获得了服务器正在运行.我还设法从客户端连接.但是,我注意到服务器脚本冻结,直到server:accept()获得连接.

Following LuaSocket Introduction I managed to get the server running. I also managed to connect from the client side. I noticed, however, that the server script freezes until server:accept() gets the connection.

研究

LuaSocket 参考指定:

LuaSocket Reference specifies:

使用settimeout方法或accept可能会阻塞,直到另一个客户端出现为止.

Use the settimeout method or accept might block until another client shows up.

这甚至包含在示例代码中.但是,client:settimeout(10)在local client = server:accept()之后被调用,因此脚本在到达这一点之前被阻塞.

This is even included in the example code. However, client:settimeout(10) is called after local client = server:accept() so the script gets blocked before it reaches this point.

我读到这可以通过多线程解决,但这似乎有点夸张.

I read that this could be worked around by multithreading but this seems to be an exaggeration.

问题

  • 如何使服务器脚本停止等待连接并继续前进?
  • 如何防止client:receive()(服务器端)和tcp:receive()(客户端)(或client:settimeout(10)负责)类似的问题?
  • How do you cause the server script to stop waiting for connection and move on?
  • How do I guard from similar problems with client:receive() (server side) and tcp:receive() (client side) (or client:settimeout(10) takes care of that)?
  • 代码

    服务器(来自LuaSocket 简介)

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

    客户端(遵循此答案)

    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:timeout(),然后调用server:accept():

    You should be able to use server:timeout() before calling server:accept():

    server:settimeout(2) local client, err = server:accept() print(client, err)

    如果2秒钟内没有请求,这将为我打印nil timeout.

    This prints nil timeout for me if no request comes in 2 seconds.

    更多推荐

    LuaSocket服务器:accept()超时(TCP)

    本文发布于:2023-11-15 21:33:17,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1598141.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:服务器   LuaSocket   TCP   accept

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!