Lua TCP/IP简单客户端服务器连接 [英] Lua TCP/IP simple Client Server connection

查看:1488
本文介绍了Lua TCP/IP简单客户端服务器连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Lua中的简单客户端-服务器连接.由于不良的在线文档,我很无奈.我在stackoverflow中找到了两个线程,但是它们并没有太大帮助.这是我到目前为止的内容:

I'm looking for a simple client-server connection in Lua. Due to bad online documentation I'm quite helpless. I found two threads here in stackoverflow but they didn't help much. Here is what I have so far:

客户:

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

推荐答案

这是工作的客户端/服务器示例,它基于 luasocket文档和这样的答案.如果您无法正常使用它,则需要提供有关这些问题的具体详细信息.

Here is a working client/server example, which is based on luasocket documentation and SO answers. If you have issues with getting it to work, you need to provide specific details about those issues.

这篇关于Lua TCP/IP简单客户端服务器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆