Discussion:
Lua socket programming examples
Jayanth Acharya
2011-03-06 17:46:38 UTC
Permalink
While the 'reference' section of the LuaSocket extension lib is nice and
informative, what I am really missing (as a Lua newbie) is something similar
to the Beej's Network Programming in C tutorial. Is there something similar
available ? Or some examples that I could study, which allows me to focus on
the nuances of network programming, without having to learn too much of
anything else at the moment ?

What I would like to understand is how to deal with receive buffers in Lua,
how to to stitch together segmented TCP data to identify message boundaries
(given message-length in the application protocol) etc. The message is all
binary data, and need to do lot of byte-order (htonX / ntohX) corrections
etc. I'd prefer the example to be pure Lua, and using LuaSocket.

regards,
Jay
Petite Abeille
2011-03-06 18:08:19 UTC
Permalink
Post by Jayanth Acharya
'd prefer the example to be pure Lua, and using LuaSocket.
Have you looked under luasocket/etc, sample and test directory? There are quite a few usage examples there, no?
Jayanth Acharya
2011-03-07 03:08:10 UTC
Permalink
Post by Petite Abeille
Post by Jayanth Acharya
'd prefer the example to be pure Lua, and using LuaSocket.
Have you looked under luasocket/etc, sample and test directory? There are
quite a few usage examples there, no?
Had not looked there. Not sure if this is because at the moment I am working
on a Windows PC, the only examples I see are under here:
C:\Program Files\Lua\5.1\examples\luasocket
and I didn't find any "etc/" directory there.

Also, the only examples seem to be fairly simple ones (as far as the network
programming aspects are concerned), though quite short and elegant ones at
that. I am lookingly for slightly more elaborate / real-life examples of
some real-life TCP based (hopefuly binary) application protocol
implementation.
Petite Abeille
2011-03-07 18:30:32 UTC
Permalink
Post by Jayanth Acharya
Had not looked there. Not sure if this is because at the moment I am working
C:\Program Files\Lua\5.1\examples\luasocket
and I didn't find any "etc/" directory there.
Take a look at the original distribution:

http://w3.impa.br/~diego/software/luasocket/
http://luaforge.net/projects/luasocket/
Post by Jayanth Acharya
Also, the only examples seem to be fairly simple ones (as far as the network
programming aspects are concerned), though quite short and elegant ones at
that. I am lookingly for slightly more elaborate / real-life examples of
some real-life TCP based (hopefuly binary) application protocol
implementation.
Hard to tell binary from text these days, but for what's worth, here is a little routine to read HTTP chunked transfer encoding:

http://dev.alt.textdrive.com/browser/HTTP/HTTP.lua#L128

And the corresponding read( ... ) method:

http://dev.alt.textdrive.com/browser/HTTP/TCPServer.lua#L111

Sam Roberts
2011-03-07 04:58:08 UTC
Permalink
Post by Jayanth Acharya
What I would like to understand is how to deal with receive buffers in Lua,
Not sure what this means.
Post by Jayanth Acharya
how to to stitch together segmented TCP data to identify message boundaries
(given message-length in the application protocol) etc. The message is all
Look at the receive() docs for tcp. You can do a read, specifying the
size of the fixed size header.

Then use struct or pack (search on luaforge) to decode the size from
the header, and read
the rest of the PDU.

luasocket will read repeatedly until it gets end-of-stream or the
amount of data you asked for. If you are dealing with multiple
streams, use select, set timeouts, and collect and concatenate the
partial data.
Post by Jayanth Acharya
binary data, and need to do lot of byte-order (htonX / ntohX) corrections
etc. I'd prefer the example to be pure Lua, and using LuaSocket.
After you try you'll be well positioned to repost with specific questions.

Cheers,
Sam
Continue reading on narkive:
Loading...