【问题标题】:WSAEWOULDBLOCK 10035WSAEWOULDBLOCK 10035
【发布时间】:2021-02-12 05:03:31
【问题描述】:

我正在尝试建立客户端-服务器连接,我对套接字等不太了解,所以我使用 github 示例进行 luajitsocket 并且我收到错误“无法立即完成非阻塞套接字操作。”

所以我什至不知道我该如何解决这个问题,这就是我在这里问的原因

我的代码:

local port = 8080
local address = socket.find_first_address("*", port)

do -- server
    local server = assert(socket.create("inet", "dgram", "udp"))
    assert(server:set_blocking(false))
    assert(server:bind(address))
    print("hosting at ", address:get_ip(), address:get_port())

    function update_server()
        local data, addr = server:receive_from()

        if data then
            print(data)
            assert(server:send_to(addr, "hello from server " .. os.clock()))
        elseif addr ~= "timeout" then
            error(addr)-- here
        end
    end
end

do -- client
    local client = assert(socket.create("inet", "dgram", "udp"))
    assert(client:set_blocking(false))
    local next_send = 0

    function update_client()
        if next_send < os.clock() then
            assert(client:send_to(address, "hello from client " .. os.clock()))
            next_send = os.clock() + math.random() + 0.5
        end

        local data, addr = client:receive_from(address)

        if data then
            print(data, addr:get_ip(), addr:get_port())
        elseif addr ~= "timeout" then
            error(addr)
        end
    end
end

while true do
    update_server()
    update_client()
end

取自:https://github.com/CapsAdmin/luajitsocket/blob/master/examples/udp_client_server.lua

我在 google 上查找此错误,但找不到任何可行的解决方案。提前致谢!

【问题讨论】:

  • 客户端代码没有意义。它是为处理接收超时而编写的,但在非阻塞模式下您无法获得读取超时。你确定你复制的正确吗?您应该删除行 assert(server:set_blocking(false)) 并改为设置您想要的任何接收超时,或者如果没有则无。
  • 使用Winsock连接服务器和客户端可以参考官方示例:Complete Winsock Client Code,Running the Winsock Client and Server Code Sample

标签: sockets winapi lua luajit


【解决方案1】:

set_blocking(false)之后的那个返回码不应该被认为是异常的。

你可能是从receive_from() 那里得到的,你不应该经常调用它,你应该在select()poll() 告诉你套接字有数据等待时这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多