Blocking IPv4 TCP on the Socket class, over libc FFI.
First consumer: x/logo/serve.x (whose Darwin-only constants this replaces). Recv truncates at the first NUL byte (ptr->str) – fine for text protocols; recv-bytes is the lossless byte-list door (#374).
SocketBlocking IPv4 TCP over libc FFI: listen/accept on the server side, connect on the client side, send/recv/close on both.
No DNS: hosts are dotted quads (“127.0.0.1”). Failures raise tag ‘io Errs with errno detail; recv answers nil at orderly EOF (absence, not a sentinel).
(Socket tcp-listen port . backlog)Create a TCP server socket: socket + SO_REUSEADDR + bind(INADDR_ANY, port) + listen.
Parameters:
INT — Port to bindINT — Listen backlog; default 16Returns: INT — The listening file descriptor
(Socket accept fd)Block until a client connects; the peer address is discarded (stat the fd’s peer later if wanted).
Parameters:
INT — Listening file descriptorReturns: INT — The connected client file descriptor
(Socket tcp-connect host port)Open a TCP connection to host:port (no DNS – dotted quads only).
Parameters:
STRING — Dotted-quad IPv4 addressINT — Port to connect toReturns: INT — The connected file descriptor
(Socket send fd s)Send the whole string; raises on failure.
Parameters:
INT — Connected file descriptorSTRING — Bytes to sendReturns: INT — Bytes sent
(Socket recv fd maxlen)Receive up to maxlen bytes as a string; nil at orderly EOF (the peer closed); raises on failure.
Parameters:
INT — Connected file descriptorINT — Maximum bytes to receiveReturns: ANY — The received string, or nil at EOF
(Socket resolve name)The host’s first IPv4 address as a dotted quad, via getaddrinfo (#412) – the DNS door; every other Socket method still takes quads. Raises tag ‘io with the gai code when resolution fails, tag ‘value when the name has no IPv4 address.
Parameters:
STRING — Hostname to resolveReturns: STRING — A dotted quad, e.g. “140.82.114.3”
(Socket recv-bytes fd maxlen)Receive up to maxlen bytes as a BYTE LIST – the lossless door (recv’s string return truncates at the first NUL; this one carries binary intact, #374); nil at orderly EOF; raises on failure.
Parameters:
INT — Connected file descriptorINT — Maximum bytes to receiveReturns: ANY — Byte list (0-255 values), or nil at EOF
(Socket close fd)Close a socket file descriptor.
Parameters:
INT — File descriptor to closeReturns: ANY — nil
(Socket local-port fd)The local port fd is bound to – the one the kernel chose when the bind asked for port 0. TCP and UDP alike.
Parameters:
INT — A bound or listening file descriptorReturns: INT — The bound port, host order
(Socket udp-bind port)Create a UDP socket bound to port on all interfaces – the receiving end. Read with (Socket recv-from fd n) for sender identity, or plain (Socket recv) when it does not matter.
Parameters:
INT — Port to bind (INADDR_ANY)Returns: INT — The bound datagram file descriptor
(Socket udp-connect host port)Create a CONNECTED UDP socket: the peer address is fixed once, and the plain (Socket send)/(Socket recv) pair then works datagram-wise – the request/reply shape. For unconnected sends use (Socket send-to).
Parameters:
STRING — Dotted-quad IPv4 addressINT — Destination portReturns: INT — The connected datagram file descriptor
(Socket send-to fd s host port)Send one datagram to host:port through an unconnected UDP socket.
Parameters:
INT — Datagram file descriptorSTRING — Bytes to send (one datagram)STRING — Dotted-quad IPv4 destinationINT — Destination portReturns: INT — Bytes sent
(Socket recv-from fd maxlen)Block for one datagram; return it WITH the sender’s identity. Like recv, the payload string truncates at the first NUL byte (ptr->str) – fine for text protocols.
Parameters:
INT — Bound datagram file descriptorINT — Maximum bytes to receiveReturns: PAIR — (payload . (host . port))
(Socket %sockaddr-un path)Build a malloc’d sockaddr_un for path – the caller frees. Raises tag ‘value past 99 bytes (the struct’s path field is 104).
Parameters:
STRING — Filesystem socket path (under 100 bytes)Returns: PTR — The packed address (110 bytes)
(Socket unix-listen path . backlog)Create a unix-domain stream server socket at path: socket + bind + listen. The path must not already exist (bind refuses) – unlink a stale one first; accept/send/recv/close are shared with TCP.
Parameters:
STRING — Filesystem socket path to createINT — Listen backlog; default 16Returns: INT — The listening file descriptor
(Socket unix-connect path)Open a unix-domain stream connection to the socket at path.
Parameters:
STRING — Filesystem socket path to connect toReturns: INT — The connected file descriptor