Binds s to a numeric address written as text and a port, and returns 0 on success.
family is AF_INET (the default) or AF_INET6 and must match the socket. address is parsed as numbers only, such
as 127.0.0.1 or ::1; a host name is never resolved. port is in host order, and 0 lets the OS pick a free port
that getsockname then reports. An address that does not parse, one of the other family, and a family that is
neither all return -1 without touching the socket, and get_last_socket_error() then says invalid argument (EINVAL,
WSAEINVAL). A failure of the bind itself returns the native error value, and get_last_socket_error() says why
(EADDRINUSE for a taken port).
assert(socket_init());
s := socket(AF_INET, .SOCK_STREAM, .TCP);
defer close_and_reset(*s);
assert(bind(s, "127.0.0.1", 0) == 0);
assert(bind(s, "not an address", 0) == -1);
assert(bind(s, "::1", 0) == -1);Doesn't run in the browser (skipped on wasm).
bind :: (
s: Socket,
address: string,
port: u16,
family: type_of(sockaddr_in.sin_family) = AF_INET
) -> s32
Binds s to a numeric address written as text and a port, and returns 0 on success.
family is AF_INET (the default) or AF_INET6 and must match the socket. address is parsed as numbers only, such
as 127.0.0.1 or ::1; a host name is never resolved. port is in host order, and 0 lets the OS pick a free port
that getsockname then reports. An address that does not parse, one of the other family, and a family that is
neither all return -1 without touching the socket, and get_last_socket_error() then says invalid argument (EINVAL,
WSAEINVAL). A failure of the bind itself returns the native error value, and get_last_socket_error() says why
(EADDRINUSE for a taken port).
Parameters
- s: Socket
- address: string
- port: u16
- family: type_of(sockaddr_in.sin_family)
Returns
- s32
assert(socket_init());
s := socket(AF_INET, .SOCK_STREAM, .TCP);
defer close_and_reset(*s);
assert(bind(s, "127.0.0.1", 0) == 0);
assert(bind(s, "not an address", 0) == -1);
assert(bind(s, "::1", 0) == -1);Doesn't run in the browser (skipped on wasm).
bind :: (s: Socket, address: u32, port: u16) -> s32
Binds s to an IPv4 address and a port, and returns 0 on success or the native error value.
address is in network byte order (htonl(INADDR_LOOPBACK), or 0 for every interface) and port is in host order,
where 0 asks the OS for a free port. The cause of a failure is in get_last_socket_error().
Parameters
- s: Socket
- address: u32
- port: u16
Returns
- s32
assert(socket_init());
s := socket(AF_INET, .SOCK_DGRAM, .UDP);
defer close_and_reset(*s);
assert(bind(s, htonl(INADDR_LOOPBACK), 0) == 0);Doesn't run in the browser (skipped on wasm).
bind :: (s: Socket, address: [16] u8, port: u16) -> s32
Binds s to an IPv6 address given as its 16 bytes in network order, and a port, and returns 0 on success.
port is in host order and 0 asks the OS for a free port. The all-zero address is every interface, and ::1 is a
zero array with the last byte set to 1. The cause of a failure is in get_last_socket_error(); a host without IPv6
fails when the socket is created.
Parameters
- s: Socket
- address: [16] u8
- port: u16
Returns
- s32
assert(socket_init());
s := socket(AF_INET6, .SOCK_STREAM, .TCP);
if s != INVALID_SOCKET {
defer close_and_reset(*s);
loopback: [16] u8;
loopback[15] = 1;
// A host without ::1 refuses the bind; where it exists the bind works.
if bind(s, loopback, 0) == 0 assert(listen(s, 1) == 0);
}Doesn't run in the browser (skipped on wasm).