Socket module

get_last_socket_error

procedure3 overloadsSource

The error code of the last failed socket call on this thread: WSAGetLastError on Windows, errno on Unix.

Read it straight after the call that failed. Compare it with SOCKET_WOULDBLOCK, SOCKET_WOULDBLOCK_CONNECT or the WSAE... constants.

Example
assert(socket_init());
s := socket(AF_INET, .SOCK_STREAM, .TCP);
defer close_and_reset(*s);

// Nothing listens on port 1, so the connection is refused.
address: sockaddr_in;
address.sin_family = AF_INET;
address.sin_port = htons(1);
#if OS == .WINDOWS address.sin_addr.S_un.S_addr = htonl(INADDR_LOOPBACK);
else address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
length: socklen_t = size_of(sockaddr_in);
assert(connect(s, cast(*sockaddr) *address, length) != 0);
assert(get_last_socket_error() != 0);

Doesn't run in the browser (skipped on wasm).

get_last_socket_error :: () -> W.OS_Error_Code

Returns

get_last_socket_error :: () -> Wasi.OS_Error_Code

The error code of the last failed socket call: errno.

Returns

Wasi.OS_Error_Code
get_last_socket_error :: () -> P.OS_Error_Code

The error code of the last failed socket call on this thread: errno on Unix, WSAGetLastError on Windows.

Read it straight after the call that failed, before any other call can replace it. Compare it with SOCKET_WOULDBLOCK, SOCKET_WOULDBLOCK_CONNECT or the platform's own error constants.

Returns

Example
assert(socket_init());

// A port that was bound and then released has nobody listening on it.
probe := socket(AF_INET, .SOCK_STREAM, .TCP);
assert(bind(probe, htonl(INADDR_LOOPBACK), 0) == 0);

closed: sockaddr_in;
length: socklen_t = size_of(sockaddr_in);
assert(getsockname(probe, cast(*sockaddr) *closed, *length) == 0);
close_and_reset(*probe);

client := socket(AF_INET, .SOCK_STREAM, .TCP);
defer close_and_reset(*client);

assert(connect(client, cast(*sockaddr) *closed, length) != 0);
assert(get_last_socket_error() != 0);

Doesn't run in the browser (skipped on wasm).

See also