【问题标题】:Lua TCP/IP simple Client Server connectionLua TCP/IP 简单客户端服务器连接
【发布时间】:2017-08-04 15:04:09
【问题描述】:

我正在 Lua 中寻找一个简单的客户端-服务器连接。由于糟糕的在线文档,我很无助。我在 stackoverflow 中找到了两个线程,但它们并没有太大帮助。这是我目前所拥有的:

客户:

local socket = require("socket")
local host, port = "192.168.100.47", 51515
local tcp = assert(socket.tcp())

tcp:connect(host, port);
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()

服务器:

local socket = require("socket")
local server = assert(socket.bind("*", 51515))
local tcp = assert(socket.tcp())

print(socket._VERSION)
print(tcp)

while 1 do

  local client = server:accept()

  line = client:receive()
  client:send("it works\n")

end

【问题讨论】:

  • 你有什么问题?
  • 我想在这两个服务器之间进行通信,但我不知道它是如何工作的,而且我在互联网上也找不到很多东西。那么问题来了:如何用LUA编写一个简单的TCP服务器和客户端?
  • 你已有的例子有什么问题?

标签: tcp lua server client


【解决方案1】:

这是一个 working client/server example,它基于 luasocket documentation 和 SO 答案。如果您在使用它时遇到问题,则需要提供有关这些问题的具体详细信息。

【讨论】:

    猜你喜欢
    • 2012-08-31
    • 2011-11-16
    • 2018-12-30
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多