lua 套接字客户端 [英] lua socket client

查看:58
本文介绍了lua 套接字客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Lua Socket 页面为 Socket Server 示例制作一个简单的 lua 套接字客户端.

I am trying to make a simple lua socket client for the Socket Server example, from the Lua Socket page.

服务器部分可以工作,我用 telnet 试过了.

The server part works though, I tried it with telnet.

但是客户端部分不起作用.

But the client part isn't working.

local host, port = "127.0.0.1", 100
local socket = require("socket")
local tcp = assert(socket.tcp())

tcp:connect(host, port);
tcp:send("hello world");

它只是应该连接到它,发送一些数据并接收一些作为回报.

It is only supposed to connect to it, send some data and receive some in return.

有人可以帮我解决吗?

推荐答案

您的服务器很可能每行接收一次.正如 receive 文档中所述,这是默认的接收模式.尝试在您的客户端消息中添加换行符.这样就完成了服务器上的接收:

Your server is likely receiving per line. As noted in the receive docs, this is the default receiving pattern. Try adding a newline to your client message. This completes the receive on the server:

local host, port = "127.0.0.1", 100
local socket = require("socket")
local tcp = assert(socket.tcp())

tcp:connect(host, port);
--note the newline below
tcp:send("hello world
");

while true do
    local s, status, partial = tcp:receive()
    print(s or partial)
    if status == "closed" then break end
end
tcp:close()

这篇关于lua 套接字客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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