【问题标题】:Multiple connections Lua Sockets多个连接 Lua Sockets
【发布时间】:2013-03-03 21:28:44
【问题描述】:

我正在使用 Lua 套接字和 TCP 制作类似 IRC 的聊天客户端和服务器。我想弄清楚的主要事情是如何让客户端和服务器监听消息并同时发送它们。因为在服务器上执行 socket:accept() 时,它会暂停程序,直到创建连接。有没有办法接受多个连接并将它们存储到一个表中?

【问题讨论】:

  • 你检查过GitHub上的LuaSocket示例了吗?

标签: sockets lua


【解决方案1】:

这看起来与 Copas 这样的调度员解决的问题完全一样。您应该阅读以下内容:http://keplerproject.github.com/copas/manual.html#why - 即使您不想使用 Copas,它也会帮助您弄清楚如何解决该问题。

基本上,您需要在accept() 之前使用select()。请注意,即使您这样做,实际上也不能保证accept() 会立即返回,因此您还应该使用settimeout()(请参阅http://w3.impa.br/~diego/software/luasocket/socket.html#select

【讨论】:

  • 基本上他需要做non-blocking socket IO。
【解决方案2】:

您需要在 accept() 之前设置超时以允许非阻塞套接字。根据 Lua 套接字的文档,accept() 方法将阻塞下一个连接。

默认情况下,所有 I/O 操作都是阻塞的。也就是说,对方法 send、receive 和 accept 的任何调用都将无限期地阻塞,直到操作完成。 settimeout 方法定义了 I/O 方法可以阻塞的时间限制

以下是聊天服务器的工作演示。您可以使用 telnet 连接到它。

socket = require("socket") -- import lib

-- define host and port
host = "*"
port = 8080

-- create socket and bind to an available port
server = assert(socket.bind(host, port))

-- get local port number and IP
ip, port = server:getsockname()

totalclient = 0 -- store the total connections
clients = {} -- e.g. clients[1] = 0xSocketAddress
clientnames = {} -- e.g. clientnames[1] = "john"

-- start the loop to listening connection
while true do

  -- new client connected, settimeout to not block connetcion
  server:settimeout(0.01)
  local client, err = server:accept();

  -- print the info of new client and add new client to the table
  if (not err) then
    clientname = client:receive()
    totalclient = totalclient + 1
    clients[totalclient] = client
    clientnames[totalclient] = clientname
    print(">> "..clientname.." connected from " .. tostring(client:getsockname()) .." at " ..os.date("%m/%d/%Y %H:%M:%S"))
  end

  -- loop through the client table
  for i = 1, totalclient do
    -- if there is client, listen to that client
    if (clients[i] ~= nil) then
      clients[totalclient]:settimeout(0.01) -- prevent blocking
      clientmessage, err = clients[i]:receive() -- accept data from client

      -- check if there is something sent to server, and no error occured
      if (err == nil and clientmessage ~= nil) then

        -- loop through the list of client to send broadcast message
        else
          for j = 1, totalclient do
            -- check not to send broadcast to the speaker
            if clients[i] ~= clients[j] then
              clients[j]:send(tostring("["..clientnames[i]).."]: " ..clientmessage.."\r\n")
            end
          end--for
          logdbmessage(clientnames[i], clientmessage)
        end--if


      end--if

    end--if
  end--for

end--while

【讨论】:

    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    相关资源
    最近更新 更多