cinvoke-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [cinvoke-dev] cinvoke/lua - server socket


From: Will Weisser
Subject: RE: [cinvoke-dev] cinvoke/lua - server socket
Date: Wed, 25 Oct 2006 13:35:52 -0500

Here is my version, this works on Linux with the latest cinvoke svn
code:

require("cinvoke_lua")

libc = clibrary.new("libc.so.6")

sockaddr_in = cstructure.new(
    Cshort, "sin_family",
    Cshort, "sin_port",
    Cint, "in_addr",
    Cint64, "sin_zero")

sock = libc:get_function(Cint , "socket", Cint, Cint, Cint)
lstn = libc:get_function(Cint,"listen", Cint, Cint)
bnd = libc:get_function(Cint, "bind", Cint, cinv.array(sockaddr_in),
Cint)
acpt = libc:get_function(Cint, "accept", Cint, cinv.array(sockaddr_in),
cinv.array(Cint))
inet_ntoa = libc:get_function(Cstring, "inet_ntoa", Cint)
close = libc:get_function(Cint, "close", Cint)

PORT = 47115 -- 3000 in reverse byte order

nsock = sock(2, 1, 0)
myaddr = {
    sin_family = 2,
    sin_port = PORT,
    in_addr = 0,
    sin_zero = 0
}

bnd(nsock, { myaddr }, 16)
lstn(nsock, 10)
print ("Listening...")
while 1 do
    claddr = {}
    claddr[1] = {
        sin_family = 0,
        sin_port = 0,
        in_addr = 0,
        sin_zero = 0
    }
    len = 16
    clsock = acpt(nsock, claddr, {len})

    print("Got a connection from " .. inet_ntoa(claddr[1].in_addr))

    close(clsock)
end

Note I flubbed the port because htonl() is implemented as a macro and I
didn't feel like writing my own.  Everything else was pretty
straightforward, I defined sockaddr_in instead of using the generic
sockaddr, listen takes two arguments not three, the structure is 16
bytes wide not 128, etc.  I listen on 0.0.0.0 instead of 127.0.0.1 but
you could change that easily by using inet_addr() or inet_aton().

This is an interesting exercise but if you're planning on doing any real
socket programming I'd highly suggest using one of the lua socket
libraries (http://luaforge.net/projects/luasocket/ for example); why
duplicate work someone else has already done?

        -W.W.

-----Original Message-----
From: address@hidden
[mailto:address@hidden On Behalf Of
address@hidden
Sent: Wednesday, October 25, 2006 5:56 AM
To: address@hidden
Subject: [cinvoke-dev] cinvoke/lua - server socket

I found a source for a C socket server :
http://www.cs.rpi.edu/courses/sysprog/sockets/server.c

I want to "translate" it on lua application. I wrote some lines just in
order to
create socket. But i don't know how can i use a structure sockadrr in a
lua
programme.
My lua source :
#!/usr/local/bin/lua

require("cinvoke_lua")

libc = clibrary.new("libc.so.6")

address = cstructure.new(Cint,"sa_family", Cchar(14), "adr")

sock = libc:get_function(Cint , "socket", Cint, Cint, Cint)
lstn = libc:get_function(Cint,"listen", Cint,cinv.array(Cchar), Cint)
bnd = libc:get_function(Cint, "bind", Cint,cinv.array(Cchar), Cint)
acpt = libc:get_function(Cint, "accept",Cint,cinv.array(Cchar), Cint)

address.sa_family = 1
address.adr = "127.0.0.1"
nsock = sock(2,1,0)
bnd(nsock,address,128)
lstn(nsock, address, 1)
print ("Listenning...")
while 1 do
        acpt(nsock,address,1)
end
I don't want to look for error for a first time.


_______________________________________________
cinvoke-dev mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/cinvoke-dev




reply via email to

[Prev in Thread] Current Thread [Next in Thread]