lua-users home
lua-l archive

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


On Mon, 21 Jul 2008 03:14:47 -0400 (EDT) Diego Nehab wrote:

> This is just an extension sample in the distribution. It's
> not really widely used. LuaSocket only officially supports
> things that can be done both on Windows and on Unix.
> There is no documentation because I thought it would save me
> time. :) At any rate, it is completely analogous to TCP.

ah, ok. yesterday i reconstructed the lua-interface from the code,
which is very well written and modularized, btw.  AFAICS it should run
unmodified on at least linux and the *BSDs, given the correct setting of
the macro "UNIX_HAS_SUN_LEN". what troubled me before was the fact that
only a factory function is exported, everything else is "OO-style".

>     -- send stdin through unix socket
>     socket = require"socket"
>     socket.unix = require"socket.unix"
>     c = assert(socket.unix())
>     assert(c:connect("/tmp/foo"))
>     while 1 do
>         local l = io.read()
>         assert(c:send(l .. "\n"))
>     end
>
> Do you need more help?

no, this example is enough to go on.  for the lists entertainment, i'll
add my (rough) notes:

  --- unix socket API ---

  timevals [double] in seconds since epoch, sockets are set non-blocking
  by default.  >>s<< in the following notes is a socket object obtained
  from:  "a = require("socket.unix"); s = a()".  defaults are marked
  with underscores, ie. somefunc([_"both"_|"send"|"receive"]) means:
  optional argument defaults to "both".

  - sending, receiving -

  result, err, nsent = s:send(stringdata [, start|1 [, end|-1]])
  result: number of bytes sent or nil on error
  err: errorstring, nsent: number bytes sent if result == nil

  result, err, t_elapsed = s:receive(nbytes|"*a"|"*l" [, prev_resultstring])
  nbytes: receive this many bytes, "*a": receive all there is, "*l":
  receive one line, prev_resultstring: partial result from previous
  receive operation or prefix wanted in buffer. t_elapsed only available
  if compiled with "-DLUASOCKET_DEBUG".

  - statistics -

  nrec, nsent, timediff = s:getstats()
  nrec: number of bytes received, nsent: number of bytes sent so far,
  timediff: (current-timeout - time-of-birth)

  sock = s:setstats([nrec [, nsent [, birthday ]]])
  nrec: number of bytes received, nsent: number of bytes sent so far,
  birthday: age of this object in seconds.  using this makes sense
  propably only on rare occasions.
  sock: socket object number (or socket?)

  - get and set actual socket fd -

  sock = s:getfd()
  s:setfd(sock), propably used rarely

  - check for yet unsent data -

  res = s:dirty()
  res: true if unsent data

  - connections:  server -

  sock, err = s:listen([backlog|_32_])
  sock, err = s:bind(path)
  client_conection, err = s:accept()

  - connections:  client -

  server_connection, err = s:connect(path)

  - connections: shuting down -

  sock = s:close(), shuts down both ends of the connection
  sock = s:shutdown([_"both"_|"send"|"receive"])
  shutting down only the send or receive side lets you send an EOF
  condition for the selected side of the stream.

  - timeouts and options -

  s:settimeout(seconds [, _'b'lock_|'t'otal ])

  sock, err = s:setoption(stringoption, value)
  stringoption: either of { "keepalive", "reuseaddr", "linger" }
  value: boolean or number

  network sockets have: "dontroute", "broadcast", "reuseaddr", "tcp_nodelay",
  "keepalive", "linger", "reuseaddr", "ip_multicast_ttl", "ip_multicast_loop",
  "ip_add_membership", "ip_drop_membersip"

  "on", "timeout", "multiaddr", "interface", "*dot.ed.quad.ip"

regards, clemens