Table of Contents
OpenMCL supports the socket abstraction for interprocess communication. A socket represents a connection to another process, typically (but not necessarily) a TCP/IP network connection to a client or server running on some other machine on the network.
All symbols mentioned in this documentation are exported from the CCL package. As of version 0.13, these symbols are additionally exported from the OPENMCL-SOCKET package.
A socket is created by make-socket. The type of socket created depends on the arguments to make-socket. These types are currently supported:
A buffered bi-directional stream over a TCP/IP connection. tcp-stream is a subclass of stream, and you can read and write to it using all the usual stream functions. Created by (make-socket :addess-family :internet :type :stream :connect :active ...) or by (accept-connection ...).
A buffered bi-directional stream over a "UNIX domain" connection. file-socket-stream is a subclass of stream, and you can read and write to it using all the usual stream functions. Created by (make-socket :address-family :file :type :stream :connect :active ...) or by (accept-connection ...),
A passive socket used to listen for incoming TCP/IP connections on a particular port. A listener-socket is not a stream. It doesn't support I/O. It can only be used to create new tcp-streams by accept-connection. Created by (make-socket :type :stream :connect :passive ...)
A passive socket used to listen for incoming UNIX domain connections named by a file in the local filesystem. A listener-socket is not a stream. It doesn't support I/O. It can only be used to create new file-socket-streams by accept-connection. Created by (make-socket :address-family :file :type :stream :connect :passive ...)
A socket representing a packet-based UDP/IP connection. A udp-socket supports I/O but it is not a stream. Instead, you must use the special functions send-to and receive-from to read and write to it. Created by (make-socket :type :datagram ...)
make-socket &key address-family type connect eol format remote-host remote-port local-host local-port local-filename remote-filename keepalive reuse-address nodelay broadcast linger backlog
Creates and returns a new socket
The address/protocol family of this socket. Currently only :internet (the default), meaning IP, and :file, referring to UNIX domain addresses, are supported.
One of :stream (the default) to request a connection-oriented socket, or :datagram to request a connectionless socket. The default is :stream.
This argument is only relevant to sockets of type :stream. One of :active (the default) to request a :passive to request a file or TCP listener socket.
This argument is currently ignored (it is accepted for compatibility with Franz Allegro).
One of :text (the default), :binary, or :bivalent. This argument is ignored for :stream sockets for now, as :stream sockets are currently always bivalent (i.e. they support both character and byte I/O). For :datagram sockets, the format determines the type of buffer created by receive-from.
Required for TCP streams, it specifies the host to connect to (in any format acceptable to lookup-hostname). Ignored for listener sockets. For UDP sockets, it can be used to specify a default host for subsequent calls to send-to or receive-from.
Required for TCP streams, it specifies the port to connect to (in any format acceptable to lookup-port). Ignored for listener sockets. For UDP sockets, it can be used to specify a default port for subsequent calls to for subsequent calls to send-to or receive-from.
Required for file-socket streams, it specifies the name of a file in the local filesystem (e.g., NOT mounted via NFS, AFP, SMB, ...) which names and controls access to a UNIX-domain socket.
Allows you to specify a local host address for a listener or UDP socket, for the rare case where you want to restrict connections to those coming to a specific local address for security reasons.
Specify a local port for a socket. Most useful for listener sockets, where it is the port on which the socket will listen for connections.
Required for file-listener-sockets. Specifies the name of a file in the local filesystem which is used to name a UNIX-domain socket. The actual filesystem file should not previously exist when the file-listener-socket is created; its parent directory should exist and be writable by the caller. The file used to name the socket will be deleted when the file-listener-socket is closed.
If true, enables the periodic transmission of "keepalive" messages.
If true, allows the reuse of local ports in listener sockets, overriding some TCP/IP protocol specifications. You will need this if you are debugging a server..
If true, disables Nagle's algorithm, which tries to minimize TCP packet fragmentation by introducing transmission delays in the absence of replies. Try setting this if you are using a protocol which involves sending a steady stream of data with no replies and are seeing significant degradations in throughput.
If true, requests permission to broadcast datagrams on a UDP socket.
If specified and non-nil, should be the number of seconds the OS is allowed to wait for data to be pushed through when a close is done. Only relevant for TCP sockets.
For a listener socket, specifies the number of connections which can be pending but not accepted. The default is 5, which is also the maximum on some operating systems.
accept-connection (socket listener-socket) &key wait
Extracts the first connection on the queue of pending connections, accepts it (i.e. completes the connection startup protocol) and returns a new tcp-stream or file-socket-stream representing the newly established connection. The tcp stream inherits any properties of the listener socket that are relevant (e.g. :keepalive, :nodelay, etc.) The original listener socket continues to be open listening for more connections, so you can call accept-connection on it again.
The listener-socket to listen on.
If true (the default), and there are no connections waiting to be accepted, waits until one arrives. If false, returns NIL immediately.
dotted-to-ipaddr dotted &key errorp
Converts a dotted-string representation of a host address to a 32-bit unsigned IP address.
A string representing an IP address in the "nn.nn.nn.nn" format
If true (the default) an error is signaled if dotted is invalid. If false, NIL is returned.
ipaddr-to-dotted ipaddr &key values
Converts a 32-bit unsigned IP address into octets.
A 32-bit integer representing an internet host address
If false (the default), returns a string in the form "nn.nn.nn.nn". If true, returns four values representing the four octets of the address as unsigned 8-bit integers.
ipaddr-to-hostname ipaddr &key ignore-cache
Converts a 32-bit unsigned IP address into a host name string
a 32-bit integer representing an internet host address
This argument is ignored (it is accepted for compatibility with Franz Allegro)
lookup-hostname host
Converts a host spec in any of the acceptable formats into a 32-bit unsigned IP address
Specifies the host. It can be either a host name string such as "clozure.com", or a dotted address string such as "192.168.0.1", or a 32-bit unsigned IP address such as 3232235521.
lookup-port port protocol
Finds the port number for the specified port and protocol
Specifies the port. It can be either a string, such as "http" or a symbol, such as :http, or an unsigned port number. Note that a string is case-sensitive. A symbol is lowercased before lookup.
Must be one of "tcp" or "udp".
receive-from (socket udp-socket) size &key buffer extract offset
Reads a UDP packet from a socket. If no packets are available, waits for a packet to arrive. Returns four values:
The buffer with the data
The number of bytes read
The 32-bit unsigned IP address of the sender of the data
The port number of the sender of the data
The socket to read from
Maximum number of bytes to read. If the packet is larger than this, any extra bytes are discarded.
If specified, must be either a string or a byte vector which will be used to read in the data. If not specified, a new buffer will be created (of type determined by socket-format).
If true, the subsequence of the buffer corresponding only to the data read in is extracted and returned as the first value. If false (the default) the original buffer is returned even if it is only partially filled.
Specifies the start offset into the buffer at which data is to be stored. The default is 0.
send-to (socket udp-socket) buffer size &key remote-host remote-port offset
Send a UDP packet over a socket.
The socket to write to
A vector containing the data to send. It must be either a string or a byte vector (either one is acceptable regardless of the stream format).
Number of bytes to send
The host to send the packet to, in any format acceptable to lookup-hostname. The default is the remote host specified in the call to make-socket.
The port to send the packet to, in any format acceptable to lookup-port. The default is the remote port specified in the call to make-socket.
The offset in the buffer where the packet data starts
shutdown socket &key direction
Shuts down part of a bidirectional connection. This is useful if e.g. you need to read responses after sending an end-of-file signal.
The socket to shut down (typically a tcp-stream)
One of :input to disallow further input, or :output to disallow further output.
ocket-os-fd socket
Returns the native OS's representation of the socket, or NIL if the socket is closed. On Unix, this is the Unix 'file descriptor', a small non-negative integer. Note that it is rather dangerous to mess around with tcp-stream fd's, as there is all sorts of buffering and asynchronous I/O going on above the OS level. listener-socket and udp-socket fd's are safer to mess with directly as there is less magic going on.
The socket
[Function]
remote-host socket
Returns the 32-bit unsigned IP address of the remote host, or NIL if the socket is not connected.
The socket
remote-port socket
Returns the remote port number, or NIL if the socket is not connected.
The socket
ocal-host socket
Returns 32-bit unsigned IP address of the local host.
The socket
local-port socket
Returns the local port number
The socket
socket-connect socket
Returns :active for tcp-stream, :passive for listener-socket, and NIL for udp-socket
The socket
socket-format socket
Returns the socket format as specified by the :format argument to make-socket.
The socket
socket-type socket
returns :stream for tcp-stream and listener-socket, and :datagram for udp-socket.
The socket
The class of OS errors signaled by socket functions
simple-error
socket-error-code socket-error
The OS error code of the error
the condition
socket-error-identifier socket-error
A symbol representing the error code in a more OS-independent way.
One of: :address-in-use :connection-aborted :no-buffer-space :connection-timed-out :connection-refused :host-unreachable :host-down :network-down :address-not-available :network-reset :connection-reset :shutdown :access-denied or :unknown.
the condition
socket-error-situation socket-error
A string describing the context where the error happened. On Linux, this is the name of the system call which returned the error.
the condition
close (socket socket) &key abort
The close generic function can be applied to sockets. It releases the operating system resources associated with the socket.
The socket to close
If false (the default), closes the socket in an orderly fashion, finishing up any buffered pending I/O, before closing the connection. If true, aborts/ignores pending I/O. (For listener and udp sockets, this argument is effectively ignored since there is never any buffered I/O to clean up).
[Macro]
with-open-socket (var . make-socket-args) &body body
executes body with var bound to the result of applying make-socket to make-socket-args. The socket gets closed on exit.
variable to bind
arguments suitable for passing to make-socket
body to execute