Io module

tcp_listen

procedureextensionexperimental2 overloadsSource

Listens on address (a literal such as ":8080", port 0 picks a free one; see local_address). reuse_port lets several listeners, one per thread, share the port; the kernel then spreads the connections between them.

Example↗
Io :: #import "Extensions/Io";

main :: () {
    Io.io_run(() {
        listener, error := Io.tcp_listen("127.0.0.1:0");
        assert(error == .NONE);
        defer Io.close(listener);

        address, _ := Io.local_address(listener);
        assert(address.port != 0); // the system picked a free port

        _, bad := Io.tcp_listen("not an address");
        assert(bad == .INVALID);
    });
}
tcp_listen :: (
    text: string,
    backlog := 1024,
    reuse_port := false
) -> (Tcp_Listener, Io_Error)

Parameters

text: string
backlog := 1024
reuse_port := false

Returns

tcp_listen :: (
    address: Address,
    backlog := 1024,
    reuse_port := false
) -> (Tcp_Listener, Io_Error)

The tcp_listen for an Address already built (address_v4, parse_address, resolve). It returns .INVALID for an address with no family, and the system's error (.ADDRESS_IN_USE, .PERMISSION_DENIED, ...) when the bind or listen fails. The listener is owned by the caller, who closes it with close. Its sockets are non-blocking and belong to the scheduler that made them.

Parameters

address: Address
backlog := 1024
reuse_port := false

Returns

See also