使用LuaSocket,此代码有效:
local socket = require'socket'
local server = socket.bind('*',51423)
local client = server:accept()
但这段代码失败了:
local socket = require 'socket'
local server = socket.tcp()
server:bind('*',51423)
local client = server:accept()
--> lua: /tmp/server.lua:4: calling 'accept' on bad self (tcp{server} expected, got userdata)
然而TCP bind的文档暗示后者应该可以工作,并指出:
“注意:socket.bind 函数可用,是创建服务器套接字的快捷方式。”
如何将通用“主”对象转换为服务器?
这样做的动机是希望在绑定操作本身上添加超时:
local socket = require'socket'
local server = socket.tcp()
server:settimeout(2/1000) -- Only wait 2ms when attempting to bind
server:bind('*',51423)