[ Home ]
SBCL Internals

The pages on this CLiki-driven site can be edited by anybody at any time. No warranty of any kind can therefore be made; any implied warranties of merchantability or fitness for a particular purpose are expressly disclaimed
[ Home ] [ Recent Changes ] [ About CLiki ] [ Text Formatting ]

The SB-BSD-SOCKETS package mostly duplicates the BSD C socket API, with some Lispification.

All non-CL symbols are in the SB-BSD-SOCKETS unless otherwise noted.

There are currently two types of sockets: LOCAL-SOCKET and INET-SOCKET.

Instead of the C "socket" call you MAKE-INSTANCE an object of class LOCAL-SOCKET or INET-SOCKET.

Standard initargs to this are :TYPE of either :STREAM or :DATAGRAM and :PROTOCOL of either :TCP or :UDP.

Example: (MAKE-INSTANCE 'INET-SOCKET :TYPE :STREAM :PROTOCOL :TCP)

A useful reader to know is SOCKET-FILE-DESCRIPTOR which fetches the FD of the given socket object.

At this point, like in the C API, the socket is active or passive depending on what you do next.

SOCKET-CONNECT socket &rest addresses or, SOCKET-BIND socket &rest local-addresses

The "addresses" portion of the call varies depending on the socket type.

For an INET-SOCKET: addresses = (host port) where host is a 4 element (unsigned-byte 8) VECTOR such as (vector 127 0 0 1) and port is an integer.

For a LOCAL-SOCKET: addresses = (filename) where filename is the path to the unix socket.

Remember, there is a fundamental difference between active and passive sockets: sockets which have been CONNECTed can be treated like streams. In fact, there is a function to convert them to a CL stream: SOCKET-MAKE-STREAM. Passive sockets only make sense when used with SOCKET-ACCEPT.

If you create a passive socket with SOCKET-BIND, you may then call the equivalent of "listen" in:

SOCKET-LISTEN socket backlog

where backlog is an integer.

SOCKET-ACCEPT socket

blocks until a connection is received in which case it returns an active socket.

CLOSE-SOCKET socket is obvious.

There are a variety of function accessors on sockets beginning with SOCKOPT- which read and set the various socket options.

To multiplex, use SB-UNIX:UNIX-SELECT. It has been Lispified such that it automatically creates fd_sets from lists, and it returns multiple values.

Example:

(let ((readfds-in (list (sb-sys:fd-stream-fd socket-stream)))
      ...)
  (multiple-value-bind (result readfds writefds errfds)
      (sb-unix:unix-select sb-unix:fd-setsize readfds-in write-fds-in errfds-in seconds useconds)
    ...))

Also see SB-BSD-SOCKETS Internals.


This page is presently Uncategorized?: please add appropriate topic markers? and remove this text

This page is linked from: index  

CLiki pages can be edited by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively