Next: Socket Options, Previous: Sockets Overview, Up: Networking

### 14.2 General Sockets

— Class: sb-bsd-sockets:socket

> Class precedence list: `socket, standard-object, t`

> Slots:

> - `protocol` — initarg: `:protocol`; reader: `sb-bsd-sockets:socket-protocol`

>   Protocol used by the socket. If a keyword, the symbol-name of the keyword will be passed to `get-protocol-by-name` downcased, and the returned value used as protocol. Other values are used as-is.

> - `type` — initarg: `:type`; reader: `sb-bsd-sockets:socket-type`

>   Type of the socket: `:stream` or `:datagram`.

> Common base class of all sockets, not meant to be directly instantiated.

— Generic Function: sb-bsd-sockets:socket-bind socket &rest address

> Bind `socket` to `address`, which may vary according to socket family. For the `inet` family, pass `address` and `port` as two arguments; for `file` address family sockets, pass the filename string. See also bind(2)

— Generic Function: sb-bsd-sockets:socket-accept socket

> Perform the accept(2) call, returning a newly-created connected socket and the peer address as multiple values

— Generic Function: sb-bsd-sockets:socket-connect socket &rest address

> Perform the connect(2) call to connect `socket` to a remote `peer`. No useful return value.

— Generic Function: sb-bsd-sockets:socket-peername socket

> Return the socket's peer; depending on the address family this may return multiple values

— Generic Function: sb-bsd-sockets:socket-name socket

> Return the address (as vector of bytes) and port that the socket is bound to, as multiple values.

— Generic Function: sb-bsd-sockets:socket-receive socket buffer length &key oob peek waitall dontwait element-type element-type

> Read `length` octets from `socket` into `buffer` (or a freshly-consed buffer if NIL), using recvfrom(2). If `length` is `nil`, the length of `buffer` is used, so at least one of these two arguments must be non-NIL. If `buffer` is supplied, it had better be of an element type one octet wide. Returns the buffer, its length, and the address of the peer that sent it, as multiple values. On datagram sockets, sets MSG_TRUNC so that the actual packet length is returned even if the buffer was too small.

— Generic Function: sb-bsd-sockets:socket-send socket buffer length &key address external-format oob eor dontroute dontwait nosignal external-format

> Send `length` octets from `buffer` into `socket`, using sendto(2). If `buffer` is a string, it will converted to octets according to `external-format`. If `length` is `nil`, the length of the octet buffer is used. The format of `address` depends on the socket type (for example for `inet` domain sockets it would be a list of an `ip` address and a port). If no socket address is provided, send(2) will be called instead. Returns the number of octets written.

— Generic Function: sb-bsd-sockets:socket-listen socket backlog

> Mark `socket` as willing to accept incoming connections. `backlog` defines the maximum length that the queue of pending connections may grow to before new connection attempts are refused. See also listen(2)

— Generic Function: sb-bsd-sockets:socket-open-p socket

> Return true if `socket` is open; otherwise, return false.

— Generic Function: sb-bsd-sockets:socket-close socket

> Close `socket`. May throw any kind of error that write(2) would have thrown. If `socket-make-stream` has been called, calls `close` on that stream instead

— Generic Function: sb-bsd-sockets:socket-make-stream socket &key input output element-type external-format buffering timeout element-type buffering external-format

> Find or create a `stream` that can be used for `io` on `socket` (which must be connected). Specify whether the stream is for `input`, `output`, or both (it is an error to specify neither). `element-type` and `external-format` are as per `open`. `timeout` specifies a read timeout for the stream.

— Method: sb-bsd-sockets:socket-make-stream (socket socket) &key input output (element-type (quote character)) (buffering full) (external-format default) timeout

> Default method for `socket` objects. An `element-type` of `:default` will construct a bivalent stream. Acceptable values for `buffering` are `:full`, `:line` and `:none`. Streams will have no `timeout` by default. The stream for `socket` will be cached, and a second invocation of this method will return the same stream. This may lead to oddities if this function is invoked with inconsistent arguments (e.g., one might request an input stream and get an output stream in response).

— Function: sb-bsd-sockets:socket-error where

— Generic Function: sb-bsd-sockets:non-blocking-mode socket

> Is `socket` in non-blocking mode?