MPL › Standard Library › windows/ws2_32
Windows Winsock2 declarations for startup, sockets, byte order, address resolution, and overlapped I/O.
Constants
Address and socket selection
Socket options
Shutdown modes
I/O-control codes
Winsock status codes
Extension-function identifiers
Startup-data lengths
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.
(lpOverlapped lpdwBytesReceived dwRemoteAddressLength dwLocalAddressLength dwReceiveDataLength lpOutputBuffer sAcceptSocket sListenSocket -- result) Defines the AcceptEx callback schema.
callback schema
Parameters
Outputs result: Int32 nonzero on immediate completion, or 0 on failure or pending completion.
(lpOverlapped lpdwBytesSent dwSendDataLength lpSendBuffer namelen name s -- result) Defines the ConnectEx callback schema.
callback schema
Parameters
Outputs result: Int32 nonzero on immediate completion, or 0 on failure or pending completion.
(RemoteSockaddrLength RemoteSockaddr LocalSockaddrLength LocalSockaddr dwRemoteAddressLength dwLocalAddressLength dwReceiveDataLength lpOutputBuffer --) Defines the GetAcceptExSockaddrs callback schema.
callback schema
Parameters
Pass the same lpOutputBuffer and the same three length values used for AcceptEx.
(dwFlags lpOverlapped cbTransferred dwError --) Defines the overlapped completion callback schema.
callback schema
Parameters
An OVERLAPPED object can be the first field of a larger caller-owned context object.
(-- WSABUF) Defines one overlapped buffer descriptor.
Dict schema
(-- WSADATA) Defines Winsock startup version, limits, vendor, and status data.
Dict schema
64-bit Windows
32-bit Windows
(-- addrinfo) Defines one address-resolution result record.
Dict schema
(-- fd_set) Defines one descriptor set for readiness queries.
Dict schema
(-- sockaddr) Defines the generic socket-address record.
Dict schema
(-- sockaddr_in) Defines the IPv4 socket-address record.
Dict schema
AF_INET is Int32 : cast it before storing it in sin_family, for example AF_INET addr.sin_family cast @addr.!sin_family.
(-- timeval) Defines the timeout interval record used by
select.
Dict schema
Imported functions
Stack effects list a C function's parameters with the first parameter on top; see importFunction .
(-- result) Cleans up one successful Winsock initialization.
Inputs
Outputs result: Int32 0 on success, -1 on failure.
(-- result) Returns the calling thread's last Winsock error code.
Inputs
Outputs result: Int32 last Winsock error code.
(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.
(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
(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 .
(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 .
(iError --) Sets the calling thread's last Winsock error code.
Inputs iError: Int32 Winsock error code to store.
Outputs
(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
(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.
(s -- result) Closes a socket.
Inputs s: Natx socket handle to close.
Outputs result: Int32 0 on success, -1 on failure.
(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.
(pAddrInfo --) Releases an address-resolution result list.
Inputs pAddrInfo: addrinfo Ref address-resolution list to release.
Outputs
(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.
(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.
(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.
(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.
(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.
(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.
(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).
(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.
(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.
(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.
(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.
(protocol type af -- result) Creates a socket.
Inputs
protocol: Int32 protocol selector.
type: Int32 socket-type selector.
af: Int32 address-family selector.
Outputs
Notes
After a failure indication, WSAGetLastError provides the Winsock error code; WSAStartup and getaddrinfo return error codes directly.
WSA_IO_PENDING from WSAGetLastError means that the overlapped request was accepted for later completion.
Obtain a pending operation's final result through its completion notification or WSAGetOverlappedResult before reusing its OVERLAPPED storage.
Keep every byte buffer referenced by a pending WSARecv or WSASend call valid at a fixed address until completion.
For completion-port notification, pass a NIL completion routine.
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
windows/dispatcher : Windows completion-port dispatcher and callback posting helpers.
windows/kernel32 : Kernel32 declarations for synchronization, process, thread, file, and timing helpers.
linux/socket · macos/socket : Linux and macOS socket declarations, constants, address schemas, and imported functions.
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.