Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua socket client

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

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.

Can someone help me fix it?

like image 273
user1058431 Avatar asked Jan 26 '12 02:01

user1058431


People also ask

What is Lua socket?

LuaSocket is a Lua extension library composed of two parts: a set of C modules that provide support for the TCP and UDP transport layers, and. a set of Lua modules that provide functions commonly needed by applications that deal with the Internet.

What is a TCP connection?

Transmission Control Protocol (TCP) – a connection-oriented communications protocol that facilitates the exchange of messages between computing devices in a network. It is the most common protocol in networks that use the Internet Protocol (IP); together they are sometimes referred to as TCP/IP.

Does WebSocket use TCP?

The WebSocket protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request. By default the WebSocket protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over TLS [RFC2818].


1 Answers

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\n");

while true do
    local s, status, partial = tcp:receive()
    print(s or partial)
    if status == "closed" then break end
end
tcp:close()
like image 165
Corbin March Avatar answered Oct 13 '22 21:10

Corbin March