Function listen [src]
Prototype
pub fn listen(address: Address, options: ListenOptions) ListenError!Server
Parameters
address: Address
options: ListenOptions
Possible Errors
Permission to create a socket of the specified type and/or pro‐tocol is denied.
The implementation does not support the specified address family.
The given address is already in use, or in the case of Internet domain sockets, The port number was specified as zero in the socket address structure, but, upon attempting to bind to an ephemeral port, it was determined that all port numbers in the ephemeral port range are currently in use. See the discussion of /proc/sys/net/ipv4/ip_local_port_range ip(7).
A nonexistent interface was requested or the requested address was not local.
Already connected
The file descriptor sockfd does not refer to a socket.
A component in the directory prefix of the socket pathname does not exist.
The option is not supported by the protocol.
addr is too long.
The network subsystem has failed.
A component of the path prefix is not a directory.
The socket is not of a type that supports the listen() operation.
Setting the socket option requires more elevated permissions.
The per-process limit on the number of open file descriptors has been reached.
Unknown protocol, or protocol family not available.
The protocol type or the specified protocol is not supported within this domain.
The socket inode would reside on a read-only filesystem.
Socket has not been bound yet
The socket type is not supported by the protocol.
Too many symbolic links were encountered in resolving addr.
The system-wide limit on the total number of open files has been reached.
Insufficient memory is available. The socket cannot be created until sufficient resources are freed.
The send and receive timeout values are too big to fit into the timeout fields in the socket structure.
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
Source
pub fn listen(address: Address, options: ListenOptions) ListenError!Server {
const nonblock: u32 = if (options.force_nonblocking) posix.SOCK.NONBLOCK else 0;
const sock_flags = posix.SOCK.STREAM | posix.SOCK.CLOEXEC | nonblock;
const proto: u32 = if (address.any.family == posix.AF.UNIX) 0 else posix.IPPROTO.TCP;
const sockfd = try posix.socket(address.any.family, sock_flags, proto);
var s: Server = .{
.listen_address = undefined,
.stream = .{ .handle = sockfd },
};
errdefer s.stream.close();
if (options.reuse_address or options.reuse_port) {
try posix.setsockopt(
sockfd,
posix.SOL.SOCKET,
posix.SO.REUSEADDR,
&mem.toBytes(@as(c_int, 1)),
);
if (@hasDecl(posix.SO, "REUSEPORT") and address.any.family != posix.AF.UNIX) {
try posix.setsockopt(
sockfd,
posix.SOL.SOCKET,
posix.SO.REUSEPORT,
&mem.toBytes(@as(c_int, 1)),
);
}
}
var socklen = address.getOsSockLen();
try posix.bind(sockfd, &address.any, socklen);
try posix.listen(sockfd, options.kernel_backlog);
try posix.getsockname(sockfd, &s.listen_address.any, &socklen);
return s;
}