TcpListener

struct TcpListener

A blocking IPv4 TCP listener backed by a host file descriptor.

bind listens on all interfaces with backlog 128. Setup failures remain in the descriptor value rather than a Result, and accept may therefore return an invalid TcpStream that callers should check.

Properties

let fd: Int

The host file descriptor.

Methods

func bind(port: Int) -> TcpListener

Creates an IPv4 socket, binds all interfaces on port, and starts listening.

Bind and listen replies are currently discarded. The returned descriptor may therefore be non-negative even when setup did not complete successfully.

View Source
pub func bind(port: Int) -> TcpListener {
	let fd = _io_socket(domain: AF_INET, socktype: SOCK_STREAM, proto: 0)
	_io_bind(fd: fd, addr: 0, port: port)
	_io_listen(fd: fd, backlog: 128)
	TcpListener(fd: fd)
}

func accept() -> TcpStream

Performs one blocking accept and wraps its returned descriptor.

A host error becomes a TcpStream with a negative descriptor; check is_valid before using it.

View Source
pub func accept() -> TcpStream {
	let client_fd = _io_accept(fd: self.fd)
	TcpStream(fd: client_fd)
}

func close() -> Int

Requests one host close and returns zero or a negative errno.

The stored descriptor is not invalidated, so callers must avoid repeated closes and later accepts through this value.

View Source
pub func close() -> Int {
	_io_close(fd: self.fd)
}