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?
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.
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.
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].
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With