MPL › Standard Library › linux/socket · macos/socket
Linux and macOS IPv4 socket declarations for addresses, resolution records, raw options, connection lifecycle, byte order, and I/O.
Functions that report failure with -1 set errno ; getaddrinfo returns an address-resolution error code instead; for its system-error code, errno holds the cause.
Constants
Address families
Socket types and protocols
Socket options
Flags
Shutdown modes
File-status commands
Address and descriptor values
Address and lookup schemas
(-- addrinfo) Defines one address-resolution result record.
Dict schema
Linux
Known issue : on 64-bit Linux, this binding declares ai_addrlen as Natx while the native field is a 32-bit length followed by padding, so a read of ai_addrlen from a record that getaddrinfo filled includes the padding bytes. Read the length through a socklen_t view, as in info.ai_addrlen socklen_t reinterpret.
macOS
Record-address fields are viewed with addressToReference , as in info.ai_addr sockaddr addressToReference and info.ai_next addrinfo addressToReference.
(-- in_addr) Aliases
Nat32 for IPv4 address storage.
Nat32 alias
The address value is represented in network byte order when passed to the socket API.
(-- sockaddr) Defines the generic socket-address record.
Dict schema
Linux
macOS
(-- sockaddr_in) Defines the IPv4 socket-address record.
Dict schema
Linux
macOS
Where a sockaddr is required, pass addr sockaddr reinterpret to bind and connect and @addr sockaddr reinterpret as the writable storage of accept ; view a sockaddr peer record as peer sockaddr_in reinterpret.
AF_INET is Int32 : cast it before storing it in sin_family, for example AF_INET addr.sin_family cast @addr.!sin_family.
(-- socklen_t) Aliases
Nat32 for socket lengths.
Nat32 alias
Imported functions
Stack effects list a C function's parameters with the first parameter on top; see importFunction .
(addrlen addr sockfd -- result) Accepts one pending connection and returns its descriptor.
Inputs
addrlen: socklen_t Ref caller-owned storage initialized to the capacity of addr in bytes and receiving the actual peer-address length.
addr: sockaddr Ref caller-owned writable peer-address storage; the call fills it.
sockfd: Int32 socket descriptor to accept from.
Outputs result: Int32 accepted socket descriptor, or -1 on failure.
(addrlen addr sockfd -- result) Associates a socket with a local address.
Inputs
Outputs result: Int32 0 on success or -1 on failure.
(addrlen addr sockfd -- result) Connects a socket to a peer address.
Inputs
addrlen: socklen_t byte size of addr.
addr: sockaddr Cref borrowed read-only peer socket address.
sockfd: Int32 socket descriptor.
Outputs result: Int32 0 on success or -1 on failure.
(res --) Releases an address-resolution result list.
Inputs
Outputs
(res hints service node -- result) Resolves a node and service into an address-resolution result list.
Inputs
res: Natx address passed as an integer to writable storage that receives the address of the first result record; view that address with addrinfo addressToReference and pass the reference to freeaddrinfo .
hints: addrinfo Cref borrowed read-only address-resolution hints, whose ai_addrlen, ai_addr, ai_canonname, and ai_next must be zero.
service: Natx address of a zero-terminated byte string naming the service, or 0nx for none.
node: Natx address of a zero-terminated byte string naming the node, or 0nx when service is provided.
Outputs result: Int32 0 on success or a nonzero address-resolution error code.
(option_len option_value option_name level socket -- result) Reads one socket option.
Inputs
option_len: socklen_t Ref caller-owned storage initialized to the capacity of the buffer at option_value in bytes and receiving the returned option-value length.
option_value: Natx address of caller-owned storage that receives the requested option value.
option_name: Int32 option identifier.
level: Int32 option level.
socket: Int32 socket descriptor.
Outputs result: Int32 0 on success or -1 on failure.
(hostlong -- result) Converts a 32-bit value from host byte order to network byte order.
Inputs hostlong: Nat32 host-order 32-bit value.
Outputs result: Nat32 network-order 32-bit value.
(hostshort -- result) Converts a 16-bit value from host byte order to network byte order.
Inputs hostshort: Nat16 host-order 16-bit value, typically a port.
Outputs result: Nat16 network-order 16-bit value.
(backlog sockfd -- result) Marks a socket as a listener with a backlog.
Inputs
backlog: Int32 requested pending-connection queue length.
sockfd: Int32 socket descriptor.
Outputs result: Int32 0 on success or -1 on failure.
(netlong -- result) Converts a 32-bit value from network byte order to host byte order.
Inputs netlong: Nat32 network-order 32-bit value.
Outputs result: Nat32 host-order 32-bit value.
(flags len buf sockfd -- result) Receives bytes from a socket.
Inputs
flags: Int32 receive flags.
len: Natx maximum number of bytes to receive.
buf: Natx address passed as an integer to caller-owned writable byte storage.
sockfd: Int32 socket descriptor.
Outputs result: Intx number of bytes received, or -1ix on failure (0ix marks orderly peer shutdown only for stream receives with a nonzero length).
(flags len buf sockfd -- result) Sends bytes through a socket.
Inputs
flags: Int32 send flags; on Linux, pass MSG_NOSIGNAL as MSG_NOSIGNAL Int32 cast.
len: Natx number of bytes to send.
buf: Natx address passed as an integer to caller-owned bytes.
sockfd: Int32 socket descriptor.
Outputs result: Intx number of bytes sent, or -1ix on failure.
(option_len option_value option_name level socket -- result) Writes one socket option.
Inputs
option_len: socklen_t option-value length in bytes.
option_value: Natx address passed as an integer to caller-owned option bytes.
option_name: Int32 option identifier.
level: Int32 option level.
socket: Int32 socket descriptor.
Outputs result: Int32 0 on success or -1 on failure.
(how sockfd -- result) Disables part or all of a socket connection.
Inputs
Outputs result: Int32 0 on success or -1 on failure.
(protocol type domain -- result) Creates a socket and returns its descriptor.
Inputs
protocol: Int32 protocol selector.
type: Int32 socket-type selector.
domain: Int32 address-family selector such as AF_INET .
Outputs result: Int32 socket descriptor, or -1 on failure.
Examples
Constant example
"control" use
"linux/socket" use
{} () {} [
AF_INET printStack _:;
SOCK_STREAM printStack _:;
SHUT_WR printStack _:;
INADDR_ANY printStack _:;
] "main" exportFunction
Expected Output During Compilation
2
1
1
0n32
Signature examples
"control" use
"linux/socket" use
{} Int32 {} [
@getaddrinfo printStack _:;
@recv printStack _:;
@socket printStack _:;
0
] "main" exportFunction
Expected Output During Compilation
{node: Natx; service: Natx; hints: {ai_flags: Int32; ai_family: Int32; ai_socktype: Int32; ai_protocol: Int32; ai_addrlen: Natx; ai_addr: Natx; ai_canonname: Natx; ai_next: Natx;} Cref; res: Natx;} Int32 {convention: "";} codeRef
{sockfd: Int32; buf: Natx; len: Natx; flags: Int32;} Intx {convention: "";} codeRef
{domain: Int32; type: Int32; protocol: Int32;} Int32 {convention: "";} codeRef
Byte-order helpers
"String" use
"control" use
"socket" use
{} Int32 {} [
(0x01020304n32 htonl ntohl 0x01020304n32 = toString LF) printList
0
] "main" exportFunction
Expected Output
TRUE
Descriptor creation example
"String" use
"control" use
"linux/posix" use
"linux/socket" use
{} Int32 {} [
sock: IPPROTO_TCP SOCK_STREAM AF_INET socket;
("created=" sock 0 < ~ LF
"close=" sock close 0 = LF) printList
0
] "main" exportFunction
Expected Output
created=TRUE
close=TRUE
See also
linux/errno · macos/errno · windows/errno : Accessor for platform error-number storage.
linux/posix · macos/posix : POSIX process, timing, descriptor, and context declarations.
sync/sync : Cross-platform scheduling, sleep, time, IPv4 formatting, and TCP helpers.
sync/TcpAcceptor : Listening TCP acceptor with address reporting and accepted connection creation.
sync/TcpConnection : Connected TCP stream with buffered read, string read, write, and shutdown operations.
windows/ws2_32 : Winsock2 declarations for sockets, overlapped I/O, and address-resolution helpers.