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 and getaddrinfo_a return an address-resolution error code instead; for the system-error code, errno holds the cause.
Constants
Address families
| Name | Linux | macOS | Meaning |
|---|---|---|---|
AF_INET | 2 | 2 | IPv4 address family. |
Address-resolution status codes and modes
| Name | Linux | macOS | Meaning |
|---|---|---|---|
EAI_AGAIN | -3 | — | Temporary address-resolution failure. |
EAI_CANCELED | -101 | — | Address-resolution request was canceled. |
EAI_INPROGRESS | -100 | — | Address-resolution request is in progress. |
EAI_SYSTEM | -11 | — | System error. |
GAI_NOWAIT | 1 | — | getaddrinfo_a mode that returns after queueing the requests. |
Socket types and protocols
| Name | Linux | macOS | Meaning |
|---|---|---|---|
SOCK_STREAM | 1 | 1 | Stream socket type. |
IPPROTO_TCP | 6 | 6 | TCP protocol selector. |
Socket options
| Name | Linux | macOS | Meaning |
|---|---|---|---|
SOL_SOCKET | 1 | 0xFFFF | Socket-level option level. |
SO_ERROR | 4 | 0x1007 | Pending socket-error option. |
SO_REUSEADDR | 2 | 0x0004 | Address-reuse option. |
SO_NOSIGPIPE | — | 0x1022 | macOS socket option that suppresses SIGPIPE. |
TCP_NODELAY | 1 | 1 | Disables Nagle's algorithm for a TCP socket. |
Flags
| Name | Linux | macOS | Meaning |
|---|---|---|---|
MSG_NOSIGNAL | 0x4000n32 | — | Linux send flag that suppresses SIGPIPE. |
Shutdown modes
| Name | Linux | macOS | Meaning |
|---|---|---|---|
SHUT_WR | 1 | 1 | Shutdown selector for the write direction. |
File-status commands
| Name | Linux | macOS | Meaning |
|---|---|---|---|
F_GETFL | 3 | 3 | File-status query command for fcntl. |
F_SETFL | 4 | 4 | File-status update command for fcntl. |
Address and descriptor values
| Name | Linux | macOS | Meaning |
|---|---|---|---|
INADDR_ANY | 0x00000000n32 | — | Linux wildcard IPv4 address. |
INVALID_SOCKET | -1 | -1 | Invalid descriptor marker used by this binding. |
SOMAXCONN | 128 | 128 | Conventional listen-backlog value. |
Address and lookup schemas
addrinfo
(-- addrinfo) Defines one address-resolution result record.Dict schema
Linux
| Field | Schema | Role |
|---|---|---|
ai_flags | Int32 | address-resolution flags |
ai_family | Int32 | address family |
ai_socktype | Int32 | socket type |
ai_protocol | Int32 | protocol selector |
ai_addrlen | Natx | address length in bytes |
ai_addr | Natx | address of the socket-address record |
ai_canonname | Natx | address of the zero-terminated canonical name |
ai_next | Natx | address of the next result record |
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
| Field | Schema | Role |
|---|---|---|
ai_flags | Int32 | address-resolution flags |
ai_family | Int32 | address family |
ai_socktype | Int32 | socket type |
ai_protocol | Int32 | protocol selector |
ai_addrlen | socklen_t | address length in bytes |
ai_canonname | Natx | address of the zero-terminated canonical name |
ai_addr | Natx | address of the socket-address record |
ai_next | Natx | address of the next result record |
- Record-address fields are viewed with
addressToReference, as ininfo.ai_addr sockaddr addressToReferenceandinfo.ai_next addrinfo addressToReference.
Dict schema; Linux only
Linux
| Field | Schema | Role |
|---|---|---|
ar_name | Natx | address of a zero-terminated byte string naming the node, or 0nx when ar_service is provided |
ar_service | Natx | address of a zero-terminated byte string naming the service, or 0nx for none |
ar_request | Natx | address of the addrinfo hints, or 0nx for none |
ar_result | Natx | address of the first result record after successful resolution |
returnValue | Int32 | request status that gai_error returns |
reserved | Int32 5 array | reserved storage |
- After successful resolution, view
ar_resultwithaddrinfo addressToReferenceand pass the reference tofreeaddrinfo.
Nat32 alias
- The address value is represented in network byte order when passed to the socket API.
sockaddr_in
(-- sockaddr_in) Defines the IPv4 socket-address record.Dict schema
Linux
| Field | Schema | Role |
|---|---|---|
sin_family | Nat16 | address family |
sin_port | Nat16 | network-order port |
sin_addr | in_addr | network-order IPv4 address |
sin_zero | Nat8 8 array | padding |
macOS
| Field | Schema | Role |
|---|---|---|
sin_len | Nat8 | address record length in bytes |
sin_family | Nat8 | address family |
sin_port | Nat16 | network-order port |
sin_addr | in_addr | network-order IPv4 address |
sin_zero | Nat8 8 array | padding |
- Where a
sockaddris required, passaddr sockaddr reinterprettobindandconnectand@addr sockaddr reinterpretas the writable storage ofaccept; view asockaddrpeer record aspeer sockaddr_in reinterpret. AF_INETisInt32: cast it before storing it insin_family, for exampleAF_INET addr.sin_family cast @addr.!sin_family.
Imported functions
Stack effects list a C function's parameters with the first parameter on top; see the stack-order rule.
accept
(addrlen addr sockfd -- result) Accepts one pending connection and returns its descriptor.Inputs
addrlen:socklen_tRefcaller-owned storage initialized to the capacity ofaddrin bytes and receiving the actual peer-address length.addr:sockaddrRefcaller-owned writable peer-address storage; the call fills it.sockfd:Int32socket descriptor to accept from.
Outputs
result:Int32accepted socket descriptor, or-1on failure.
bind
(addrlen addr sockfd -- result) Associates a socket with a local address.Inputs
addrlen:socklen_tbyte size ofaddr.addr:sockaddrCrefborrowed read-only local socket address.sockfd:Int32socket descriptor.
Outputs
result:Int320 on success or-1on failure.
connect
(addrlen addr sockfd -- result) Connects a socket to a peer address.Inputs
addrlen:socklen_tbyte size ofaddr.addr:sockaddrCrefborrowed read-only peer socket address.sockfd:Int32socket descriptor.
Outputs
result:Int320 on success or-1on failure.
Inputs
Outputs
result:Int32EAI_CANCELEDwhen the request is canceled, or another address-resolution status code.
Remarks
- Linux declaration only.
Inputs
Outputs
result:Int320 after successful resolution,EAI_INPROGRESSwhile the request is in progress, or a nonzero address-resolution status code.
Remarks
- Linux declaration only.
getaddrinfo
(res hints service node -- result) Resolves a node and service into an address-resolution result list.Inputs
res:Natxaddress passed as an integer to writable storage that receives the address of the first result record; view that address withaddrinfo addressToReferenceand pass the reference tofreeaddrinfo.hints:addrinfoCrefborrowed read-only address-resolution hints, whoseai_addrlen,ai_addr,ai_canonname, andai_nextmust be zero.service:Natxaddress of a zero-terminated byte string naming the service, or0nxfor none.node:Natxaddress of a zero-terminated byte string naming the node, or0nxwhenserviceis provided.
Outputs
result:Int320 on success or a nonzero address-resolution error code.
getaddrinfo_a
(event count list mode -- result) Submits a list of address-resolution requests.Inputs
event:sigeventRefcompletion-notification settings forGAI_NOWAITmode, or a typedNILsigevent Reffor none.count:Int32number of requests inlist.list:NatxRefcaller-owned array ofcountaddresses ofgaicbrequest records.mode:Int32GAI_NOWAITto return after queueing, or0to wait for every request.
Outputs
result:Int320 when every request is queued or a nonzero address-resolution error code.
Remarks
- Linux declaration only.
getsockopt
(option_len option_value option_name level socket -- result) Reads one socket option.Inputs
option_len:socklen_tRefcaller-owned storage initialized to the capacity of the buffer atoption_valuein bytes and receiving the returned option-value length.option_value:Natxaddress of caller-owned storage that receives the requested option value.option_name:Int32option identifier.level:Int32option level.socket:Int32socket descriptor.
Outputs
result:Int320 on success or-1on failure.
htonl
(hostlong -- result) Converts a 32-bit value from host byte order to network byte order.Inputs
hostlong:Nat32host-order 32-bit value.
Outputs
result:Nat32network-order 32-bit value.
htons
(hostshort -- result) Converts a 16-bit value from host byte order to network byte order.Inputs
hostshort:Nat16host-order 16-bit value, typically a port.
Outputs
result:Nat16network-order 16-bit value.
listen
(backlog sockfd -- result) Marks a socket as a listener with a backlog.Inputs
Outputs
result:Int320 on success or-1on failure.
ntohl
(netlong -- result) Converts a 32-bit value from network byte order to host byte order.Inputs
netlong:Nat32network-order 32-bit value.
Outputs
result:Nat32host-order 32-bit value.
recv
(flags len buf sockfd -- result) Receives bytes from a socket.Inputs
flags:Int32receive flags.len:Natxmaximum number of bytes to receive.buf:Natxaddress passed as an integer to caller-owned writable byte storage.sockfd:Int32socket descriptor.
Outputs
result:Intxnumber of bytes received, or-1ixon failure (0ixmarks orderly peer shutdown only for stream receives with a nonzero length).
send
(flags len buf sockfd -- result) Sends bytes through a socket.Inputs
flags:Int32send flags; on Linux, passMSG_NOSIGNALasMSG_NOSIGNAL Int32 cast.len:Natxnumber of bytes to send.buf:Natxaddress passed as an integer to caller-owned bytes.sockfd:Int32socket descriptor.
Outputs
result:Intxnumber of bytes sent, or-1ixon failure.
setsockopt
(option_len option_value option_name level socket -- result) Writes one socket option.Inputs
option_len:socklen_toption-value length in bytes.option_value:Natxaddress passed as an integer to caller-owned option bytes.option_name:Int32option identifier.level:Int32option level.socket:Int32socket descriptor.
Outputs
result:Int320 on success or-1on failure.
Examples
Example: constants of socket
AF_INET, SOCK_STREAM, and SHUT_WR are 2, 1, and 1, and INADDR_ANY is 0n32.
"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
Example: getaddrinfo, recv, and socket signatures
Printing @getaddrinfo, @recv, and @socket during compilation shows their imported signatures.
"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
Example: byte-order helpers
ntohl undoes htonl, so the round trip returns 0x01020304 unchanged.
"String" use
"control" use
"socket" use
{} Int32 {} [
(0x01020304n32 htonl ntohl 0x01020304n32 = toString LF) printList
0
] "main" exportFunction
Expected Output
TRUE
Example: descriptor creation
socket creates a TCP socket with a nonnegative descriptor, and close releases it with result 0.
"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.