Socket module

set_blocking

procedure · 2 overloadsSource

Switches s between blocking and non-blocking mode and returns whether it worked.

In non-blocking mode recv, send, accept and connect return an error instead of waiting, and get_last_socket_error() is SOCKET_WOULDBLOCK. Reads and writes can then move fewer bytes than asked for. A failure is logged through context.logger. New sockets start in blocking mode.

set_blocking :: (s: Socket, blocking: bool) -> bool

Switches s between blocking and non-blocking mode and returns whether it worked.

In non-blocking mode recv, send, accept and connect return an error instead of waiting, and get_last_socket_error() is SOCKET_WOULDBLOCK. Reads and writes can then move fewer bytes than asked for. A failure is logged through context.logger. New sockets start in blocking mode.

Parameters

s: Socket
blocking: bool

Returns

bool
set_blocking :: (s: Socket, blocking: bool) -> bool

Switches s between blocking and non-blocking mode and returns whether it worked.

In non-blocking mode recv, send, accept and connect return -1 (or INVALID_SOCKET) instead of waiting, and get_last_socket_error() is SOCKET_WOULDBLOCK (or SOCKET_WOULDBLOCK_CONNECT for a connect under way). Reads and writes can then move fewer bytes than asked for. A failure is logged through context.logger, and the mode is left as it was. New sockets start in blocking mode.

Parameters

s: Socket
blocking: bool

Returns

bool
Example
assert(socket_init());

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

assert(set_blocking(s, false));
assert(set_blocking(s, true));

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

See also