LuaJIT Tcp Connections

And finally, I come to the point where I can make basic TCP connections…

I started with IN_ADDR, SOCKADDR_IN, IN6_ADDR, SOCKADDR_IN6, SOCKADDR_STORAGE, etc.  Then you have to go crawling all over the Windows SDK to collect the various bits and pieces from the following headers and more: ws2tcpip.h, ws2def.h, winsock2.h, wintypes.h, winbase.h, and a couple others.

Well, at the end of the day, you can create a simple daytime client like this:

require "DaytimeClient"
local dtc = DaytimeClient("")
print("client ready to run")

Of course, that’s a cheat, because the actual work is in the DaytimeClient class. The only thing this will do is get a date from the service, and print it out. The DaytimeClient itself is fairly straight forward though:

require "BanateCore"
require "TcpSocket"


local daytimeport = 13

function DaytimeClient:_init(hostname, port)
    hostname = hostname or "localhost"
    port = port or daytimeport

    self.Socket = CreateTcpClientSocket(hostname, port)

function DaytimeClient:Run()
    local buff ="char [256]")
    local bufflen = 256

    n = self.Socket:Read(buff, bufflen)
    while (n > 0) do
        buff[n] = 0		-- null terminated

        n = self.Socket:Read(buff, bufflen)

I think that’s fairly easy. To create a client of any kind, just tell which hostname and port number to connect to, call connect, then start reading/writing, depending on your protocol.

If you want to create some sort of “server” the code is equally easy:

require "BanateCore"
require "TcpSocket"


local daytimeport = 13

function DaytimeServer:_init(hostname, port)
    port = port or daytimeport

    self.Socket = CreateTcpSocket()

function DaytimeServer:Run()
    local buff ="char [256]")
    local bufflen = 256

print("Daytime Server Running")

    while (true) do
        local acceptedsock = self.Socket:Accept()

        -- Get the current date copied into a buffer
        local dt ="%c")
        local dtlen = string.len(dt)
        memcpy(buff, dt, dtlen)
        buff[dtlen] = 0

        -- Write the buffer to the client
        acceptedsock:Write(buff, dtlen)

print("wrote back to client")

        -- close down the socket

These are just building blocks though. Now, I want to have a HTTPClient of course, and FTP, etc.

So, what’s amazing about this to me? Well, there’s no “C” code in here at all. It was all done with LuaJIT FFI interop. I wrapped up all the Winsock calls that I need, and that’s the end of that. The same can be done to port to Linux or Mac, which is nice to me because I don’t need to learn any other build systems beyond what I do with Lua. That’s a huge relief, as I often find the most difficult part of using various libraries with Lua is that even though the basic C code might be ‘portable’, the build systems associated with them are not particularly easy to use.

Why not just use LuaSocket?  Well, LuaSockets is a great package, and has been quite the workhorse for several Lua based networking projects.  So, nothing bad to say about it.  My requirements are the following though:  The code must not rely on any external build systems, it must support I/O completion ports on Windows for async communications.  The number of traversals through the interop layer must be as minimal as possible.  I think the biggest challenge for my needs is the io completion port thing.  I actually don’t even know if LuaSockets support that, but if it doesn’t already, it’s quite a lot of work to add the pattern.

At any rate, now I have the core networking. I can now slowly stitch together the async iocompletion port versions of the same, and we’ll see whether Lua will be capable of creating services that are on par with what can be created with Node.js. I’m assuming the answer is yes, and possibly even better, so that will be interesting to find out.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s