lua 套接字客户端

编程入门 行业动态 更新时间:2024-10-23 10:25:10
本文介绍了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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-29 04:08:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/747787.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   lua

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!