Windows Winsock2 declarations for startup, sockets, byte order, address resolution, and overlapped I/O.


Constants

Address and socket selection

NameValueMeaning
AF_INET2IPv4 address family.
INADDR_ANY0x00000000n32Wildcard local IPv4 address.
INVALID_SOCKETall bits set (Natx)Socket result that indicates failure.
SOCK_STREAM1Stream socket type.
IPPROTO_TCP6TCP protocol selector.

Socket options

NameValueMeaning
SOL_SOCKET0xFFFFSocket-level option level.
SOMAXCONN0x7FFFFFFFRequests the provider's maximum reasonable listen backlog.
SO_EXCLUSIVEADDRUSE-5Exclusive local-address-use option.
SO_UPDATE_ACCEPT_CONTEXT0x700BAccept-context update option.
SO_UPDATE_CONNECT_CONTEXT0x7010Connect-context update option.
TCP_NODELAY1Disables Nagle's algorithm for a TCP socket.

Shutdown modes

NameValueMeaning
SD_RECEIVE0Disables receiving on a socket.
SD_SEND1Disables sending on a socket.
SD_BOTH2Disables sending and receiving on a socket.

I/O-control codes

NameValueMeaning
FIONBIO-2147195266Sets a socket's nonblocking mode through ioctlsocket.
SIO_GET_EXTENSION_FUNCTION_POINTER0xC8000006n32Requests an extension-function pointer through WSAIoctl.

Winsock status codes

NameValueMeaning
WSAEWOULDBLOCK10035Nonblocking operation would block.
WSA_IO_PENDING997Overlapped operation is pending.

Extension-function identifiers

NameValueMeaning
WSAID_ACCEPTEX(0xB5367DF1n32 0xCBACn16 0x11CFn16 (0x95n8 0xCAn8 0x00n8 0x80n8 0x5Fn8 0x48n8 0xA1n8 0x92n8))Identifier for the AcceptEx extension function.
WSAID_CONNECTEX(0x25A207B9n32 0xDDF3n16 0x4660n16 (0x8En8 0xE9n8 0x76n8 0xE5n8 0x8Cn8 0x74n8 0x06n8 0x3En8))Identifier for the ConnectEx extension function.
WSAID_GETACCEPTEXSOCKADDRS(0xB5367DF2n32 0xCBACn16 0x11CFn16 (0x95n8 0xCAn8 0x00n8 0x80n8 0x5Fn8 0x48n8 0xA1n8 0x92n8))Identifier for the GetAcceptExSockaddrs extension function.

Startup-data lengths

NameValueMeaning
WSADESCRIPTION_LEN256Description length used by WSADATA.
WSASYS_STATUS_LEN128System-status length used by WSADATA.

Schemas

For FN_ACCEPTEXRef, FN_CONNECTEXRef, FN_GETACCEPTEXSOCKADDRSRef, and WSAOVERLAPPED_COMPLETION_ROUTINERef, each parenthesized effect describes invocation of a non-NIL Code value; evaluating these four names produces NIL Code descriptors.

FN_ACCEPTEXRef

(lpOverlapped lpdwBytesReceived dwRemoteAddressLength dwLocalAddressLength dwReceiveDataLength lpOutputBuffer sAcceptSocket sListenSocket -- result) Defines the AcceptEx callback schema.

callback schema

Parameters

ParameterSchemaRole
sListenSocketNatxlistening socket handle
sAcceptSocketNatxopen socket handle, neither bound nor connected, on which to accept the connection
lpOutputBufferNatxaddress of a caller-owned buffer receiving initial data followed by local and remote address records
dwReceiveDataLengthNat32initial receive-data length in bytes
dwLocalAddressLengthNat32local-address storage length in bytes, at least 16 bytes larger than the protocol's maximum address size
dwRemoteAddressLengthNat32remote-address storage length in bytes, at least 16 bytes larger than the protocol's maximum address size
lpdwBytesReceivedNat32 Refstorage receiving the received-byte count only on immediate completion
lpOverlappedOVERLAPPED Refrequired non-NIL overlapped operation record

Outputs

  • result: Int32 nonzero on immediate completion, or 0 on failure or pending completion.

FN_CONNECTEXRef

(lpOverlapped lpdwBytesSent dwSendDataLength lpSendBuffer namelen name s -- result) Defines the ConnectEx callback schema.

callback schema

Parameters

ParameterSchemaRole
sNatxpreviously bound, unconnected socket handle
nameNatxaddress of the destination address record
namelenInt32destination address length in bytes
lpSendBufferNatxaddress of the initial send buffer, or 0nx for none
dwSendDataLengthNat32initial send-data length in bytes
lpdwBytesSentNat32 Refstorage receiving the sent-byte count on successful return, ignored when lpSendBuffer is 0nx
lpOverlappedOVERLAPPED Refrequired non-NIL overlapped operation record

Outputs

  • result: Int32 nonzero on immediate completion, or 0 on failure or pending completion.

FN_GETACCEPTEXSOCKADDRSRef

(RemoteSockaddrLength RemoteSockaddr LocalSockaddrLength LocalSockaddr dwRemoteAddressLength dwLocalAddressLength dwReceiveDataLength lpOutputBuffer --) Defines the GetAcceptExSockaddrs callback schema.

callback schema

Parameters

ParameterSchemaRole
lpOutputBufferNatxaddress of the AcceptEx output buffer
dwReceiveDataLengthNat32initial receive-data length in bytes
dwLocalAddressLengthNat32local-address storage length in bytes
dwRemoteAddressLengthNat32remote-address storage length in bytes
LocalSockaddrsockaddr AsRef Refcaller-owned pointer-to-pointer storage; the filled AsRef wrapper's data field holds the local socket-address pointer, which points into lpOutputBuffer
LocalSockaddrLengthInt32 Refstorage receiving the local address length in bytes
RemoteSockaddrsockaddr AsRef Refcaller-owned pointer-to-pointer storage; the filled AsRef wrapper's data field holds the remote socket-address pointer, which points into lpOutputBuffer
RemoteSockaddrLengthInt32 Refstorage receiving the remote address length in bytes
  • Pass the same lpOutputBuffer and the same three length values used for AcceptEx.

WSAOVERLAPPED_COMPLETION_ROUTINERef

(dwFlags lpOverlapped cbTransferred dwError --) Defines the overlapped completion callback schema.

callback schema

Parameters

ParameterSchemaRole
dwErrorNat32completion error code
cbTransferredNat32transferred-byte count
lpOverlappedOVERLAPPED Refcompleted operation record
dwFlagsNat32completion flags
  • An OVERLAPPED object can be the first field of a larger caller-owned context object.

WSABUF

(-- WSABUF) Defines one overlapped buffer descriptor.

Dict schema

FieldSchemaRole
lenNat32buffer length in bytes
bufNatxaddress of the buffer

WSADATA

(-- WSADATA) Defines Winsock startup version, limits, vendor, and status data.

Dict schema

64-bit Windows

FieldSchemaRole
wVersionNat16negotiated Winsock version
wHighVersionNat16highest supported Winsock version
iMaxSocketsNat16legacy socket-count limit (ignored for Winsock 2 and later)
iMaxUdpDgNat16legacy UDP datagram-size limit in bytes (ignored for Winsock 2 and later)
lpVendorInfoNat8 Reflegacy vendor information (ignored for Winsock 2 and later)
szDescriptionNat8 257 arrayWinsock description string storage
szSystemStatusNat8 129 arrayWinsock system-status string storage

32-bit Windows

FieldSchemaRole
wVersionNat16negotiated Winsock version
wHighVersionNat16highest supported Winsock version
szDescriptionNat8 257 arrayWinsock description string storage
szSystemStatusNat8 129 arrayWinsock system-status string storage
iMaxSocketsNat16legacy socket-count limit (ignored for Winsock 2 and later)
iMaxUdpDgNat16legacy UDP datagram-size limit in bytes (ignored for Winsock 2 and later)
lpVendorInfoNat8 Reflegacy vendor information (ignored for Winsock 2 and later)

addrinfo

(-- addrinfo) Defines one address-resolution result record.

Dict schema

FieldSchemaRole
ai_flagsInt32address-resolution flags
ai_familyInt32address family
ai_socktypeInt32socket type
ai_protocolInt32protocol selector
ai_addrlenNatxaddress length in bytes
ai_canonnameNatxaddress of the zero-terminated canonical name
ai_addrNatxaddress of the socket-address record
ai_nextNatxaddress of the next result record

fd_set

(-- fd_set) Defines one descriptor set for readiness queries.

Dict schema

FieldSchemaRole
fd_countNat32number of descriptors
fd_arrayNatx 64 arraysocket descriptors

sockaddr

(-- sockaddr) Defines the generic socket-address record.

Dict schema

FieldSchemaRole
sa_familyNat16address family
sa_dataNat8 14 arrayraw address bytes

sockaddr_in

(-- sockaddr_in) Defines the IPv4 socket-address record.

Dict schema

FieldSchemaRole
sin_familyNat16address family
sin_portNat16port in network byte order
sin_addrNat32IPv4 address in network byte order
sin_zeroNat8 8 arraypadding
  • AF_INET is Int32: cast it before storing it in sin_family, for example AF_INET addr.sin_family cast @addr.!sin_family.

timeval

(-- timeval) Defines the timeout interval record used by select.

Dict schema

FieldSchemaRole
tv_secInt32seconds
tv_usecInt32microseconds

Imported functions

Stack effects list a C function's parameters with the first parameter on top; see importFunction.

WSACleanup

(-- result) Cleans up one successful Winsock initialization.

Inputs

  • None.

Outputs

  • result: Int32 0 on success, -1 on failure.

WSAGetLastError

(-- result) Returns the calling thread's last Winsock error code.

Inputs

  • None.

Outputs

  • result: Int32 last Winsock error code.

WSAGetOverlappedResult

(lpdwFlags fWait lpcbTransfer lpOverlapped s -- result) Retrieves the result of one overlapped socket operation.

Inputs

  • lpdwFlags: Nat32 Ref caller-owned storage that receives final operation flags.
  • fWait: Int32 nonzero to wait (event-based notification only), or 0 to return without waiting.
  • lpcbTransfer: Nat32 Ref caller-owned storage that receives the transferred-byte count.
  • lpOverlapped: OVERLAPPED Ref overlapped operation record.
  • s: Natx socket handle for the operation.

Outputs

  • result: Int32 nonzero on success, 0 on failure.

WSAIoctl

(lpCompletionRoutine lpOverlapped lpcbBytesReturned cbOutBuffer lpvOutBuffer cbInBuffer lpvInBuffer dwIoControlCode s -- result) Controls a socket and supports extension-function lookup.

Inputs

  • lpCompletionRoutine: WSAOVERLAPPED_COMPLETION_ROUTINERef completion callback, or NIL for none.
  • lpOverlapped: OVERLAPPED Ref overlapped operation record, or a typed NIL OVERLAPPED Ref for synchronous I/O.
  • lpcbBytesReturned: Nat32 Ref caller-owned storage that receives the output byte count on immediate completion.
  • cbOutBuffer: Nat32 output-buffer size in bytes.
  • lpvOutBuffer: Natx address of the output buffer, or 0nx for none.
  • cbInBuffer: Nat32 input-buffer size in bytes.
  • lpvInBuffer: Natx address of the input buffer, or 0nx for none.
  • dwIoControlCode: Nat32 socket-control code.
  • s: Natx socket handle.

Outputs

  • result: Int32 0 on success, -1 on failure; an overlapped call can return -1 with WSA_IO_PENDING.

Remarks

WSARecv

(lpCompletionRoutine lpOverlapped lpFlags lpNumberOfBytesRecvd dwBufferCount lpBuffers s -- result) Receives data from a socket, synchronously or with overlapped I/O.

Inputs

  • lpCompletionRoutine: WSAOVERLAPPED_COMPLETION_ROUTINERef completion callback, or NIL for none.
  • lpOverlapped: OVERLAPPED Ref overlapped operation record, or a typed NIL OVERLAPPED Ref for synchronous I/O.
  • lpFlags: Nat32 Ref caller-owned input/output receive flags, updated only on immediate completion; see the Windows parameter documentation for the documented flag values.
  • lpNumberOfBytesRecvd: Nat32 Ref caller-owned storage for bytes received on immediate completion. This Nat32 Ref may be NIL only when lpOverlapped is non-NIL.
  • dwBufferCount: Nat32 number of WSABUF descriptors.
  • lpBuffers: WSABUF Ref descriptor array for the receive buffers.
  • s: Natx socket handle.

Outputs

  • result: Int32 0 on success, -1 on failure; an overlapped call can return -1 with WSA_IO_PENDING.

WSASend

(lpCompletionRoutine lpOverlapped dwFlags lpNumberOfBytesSent dwBufferCount lpBuffers s -- result) Sends data through a socket, synchronously or with overlapped I/O.

Inputs

  • lpCompletionRoutine: WSAOVERLAPPED_COMPLETION_ROUTINERef completion callback, or NIL for none.
  • lpOverlapped: OVERLAPPED Ref overlapped operation record, or a typed NIL OVERLAPPED Ref for synchronous I/O.
  • dwFlags: Nat32 send flags, normally 0n32; see the Windows parameter documentation for the documented flag values.
  • lpNumberOfBytesSent: Nat32 Ref caller-owned storage for bytes sent on immediate completion. This Nat32 Ref may be NIL only when lpOverlapped is non-NIL.
  • dwBufferCount: Nat32 number of WSABUF descriptors.
  • lpBuffers: WSABUF Ref descriptor array for the send buffers.
  • s: Natx socket handle.

Outputs

  • result: Int32 0 on success, -1 on failure; an overlapped call can return -1 with WSA_IO_PENDING.

WSASetLastError

(iError --) Sets the calling thread's last Winsock error code.

Inputs

  • iError: Int32 Winsock error code to store.

Outputs

  • None.

WSAStartup

(lpWSAData wVersionRequested -- result) Initializes Winsock and fills startup data.

Inputs

  • lpWSAData: WSADATA Ref caller-owned startup-data storage that the call fills.
  • wVersionRequested: Nat16 requested Winsock version.

Outputs

  • result: Int32 0 on success, a nonzero error code on failure.

Remarks

bind

(namelen name s -- result) Associates a socket with a local address.

Inputs

  • namelen: Int32 size in bytes of the address at name.
  • name: Natx address of a borrowed read-only local socket-address record.
  • s: Natx socket handle.

Outputs

  • result: Int32 0 on success, -1 on failure.

closesocket

(s -- result) Closes a socket.

Inputs

  • s: Natx socket handle to close.

Outputs

  • result: Int32 0 on success, -1 on failure.

connect

(nameLen name s -- result) Connects a socket to a peer address.

Inputs

  • nameLen: Int32 size in bytes of the address at name.
  • name: sockaddr Ref mutable borrowed peer socket address, which the call does not modify.
  • s: Natx socket handle.

Outputs

  • result: Int32 0 on success, -1 on failure.

freeaddrinfo

(pAddrInfo --) Releases an address-resolution result list.

Inputs

  • pAddrInfo: addrinfo Ref address-resolution list to release.

Outputs

  • None.

getaddrinfo

(ppResult pHints pServiceName pNodeName -- result) Resolves a node and service into an address list.

Inputs

  • ppResult: 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.
  • pHints: addrinfo Cref borrowed address-resolution hints, whose ai_addrlen, ai_canonname, ai_addr, and ai_next must be 0nx.
  • pServiceName: Natx address of a zero-terminated byte string naming the service, or 0nx for none.
  • pNodeName: Natx address of a zero-terminated byte string naming the node, or 0nx when pServiceName is provided.

Outputs

  • result: Int32 0 on success, a nonzero error code on failure.

htonl

(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.

htons

(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.

ioctlsocket

(argp cmd s -- result) Controls socket I/O mode.

Inputs

  • argp: Natx address of caller-owned writable control-argument storage.
  • cmd: Int32 socket-control command.
  • s: Natx socket handle.

Outputs

  • result: Int32 0 on success, -1 on failure.

listen

(backlog s -- result) Places a socket in listening mode.

Inputs

  • backlog: Int32 requested pending-connection queue length.
  • s: Natx socket handle.

Outputs

  • result: Int32 0 on success, -1 on failure.

ntohl

(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.

ntohs

(netshort -- result) Converts a 16-bit value from network byte order to host byte order.

Inputs

  • netshort: Nat16 network-order 16-bit value.

Outputs

  • result: Nat16 host-order 16-bit value.

recv

(flags len buf s -- result) Receives bytes from a socket.

Inputs

  • flags: Int32 receive flags.
  • len: Int32 maximum number of bytes to receive.
  • buf: Natx address of caller-owned writable receive storage.
  • s: Natx socket handle.

Outputs

  • result: Int32 number of bytes received, or -1 on failure (0 marks orderly peer shutdown for a stream socket).

select

(timeout exceptfds writefds readfds ndfs -- result) Tests descriptor sets for socket readiness.

Inputs

  • timeout: timeval Ref timeout interval in seconds and microseconds.
  • exceptfds: fd_set Ref caller-owned exception-interest set that the call updates.
  • writefds: fd_set Ref caller-owned write-interest set that the call updates.
  • readfds: fd_set Ref caller-owned read-interest set that the call updates.
  • ndfs: Int32 value ignored on Windows.

Outputs

  • result: Int32 number of ready sockets, 0 on timeout, or -1 on failure.

send

(flags len buf s -- result) Sends bytes through a socket.

Inputs

  • flags: Int32 send flags.
  • len: Int32 number of bytes to send.
  • buf: Natx address of caller-owned bytes to send.
  • s: Natx socket handle.

Outputs

  • result: Int32 number of bytes sent, or -1 on failure.

setsockopt

(optlen optval optname level s -- result) Sets one socket option.

Inputs

  • optlen: Int32 option-value length in bytes.
  • optval: Natx address of the option value.
  • optname: Int32 option identifier.
  • level: Int32 option level.
  • s: Natx socket handle.

Outputs

  • result: Int32 0 on success, -1 on failure.

shutdown

(how s -- result) Disables part or all of a socket connection.

Inputs

  • how: Int32 shutdown selector.
  • s: Natx socket handle.

Outputs

  • result: Int32 0 on success, -1 on failure.

socket

(protocol type af -- result) Creates a socket.

Inputs

  • protocol: Int32 protocol selector.
  • type: Int32 socket-type selector.
  • af: Int32 address-family selector.

Outputs


Notes


Examples

Constant example

"control"        use
"windows/ws2_32" use

{} () {} [
  AF_INET printStack _:;
  SOCK_STREAM printStack _:;
  INVALID_SOCKET printStack _:;
  WSAEWOULDBLOCK printStack _:;
  SD_BOTH printStack _:;
] "main" exportFunction

Expected Output During Compilation

2
1
18446744073709551615nx
10035
2

Signature examples

"control"        use
"windows/ws2_32" use

{} Int32 {} [
  @WSAStartup printStack _:;
  @WSARecv printStack _:;
  @socket printStack _:;
  0
] "main" exportFunction

Expected Output During Compilation

{wVersionRequested: Nat16; lpWSAData: {wVersion: Nat16; wHighVersion: Nat16; iMaxSockets: Nat16; iMaxUdpDg: Nat16; lpVendorInfo: Nat8 Ref; szDescription: Nat8 257 array; szSystemStatus: Nat8 129 array;} Ref;} Int32 {convention: "";} codeRef
{s: Natx; lpBuffers: {len: Nat32; buf: Natx;} Ref; dwBufferCount: Nat32; lpNumberOfBytesRecvd: Nat32 Ref; lpFlags: Nat32 Ref; lpOverlapped: {Internal: Natx; InternalHigh: Natx; DUMMYUNIONNAME: Nat64; hEvent: Natx;} Ref; lpCompletionRoutine: {dwError: Nat32; cbTransferred: Nat32; lpOverlapped: {Internal: Natx; InternalHigh: Natx; DUMMYUNIONNAME: Nat64; hEvent: Natx;} Ref; dwFlags: Nat32;} () {convention: "";} codeRef;} Int32 {convention: "";} codeRef
{af: Int32; type: Int32; protocol: Int32;} Natx {convention: "";} codeRef

See also