pub struct TcpListener(/* private fields */);
Expand description
A TCP socket server, listening for connections.
After creating a TcpListener
by bind
ing it to a socket address, it listens
for incoming TCP connections. These can be accepted by calling accept
or by
iterating over the Incoming
iterator returned by incoming
.
The socket will be closed when the value is dropped.
The Transmission Control Protocol is specified in IETF RFC 793.
§Examples
use std::net::{TcpListener, TcpStream};
fn handle_client(stream: TcpStream) {
// ...
}
fn main() -> std::io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:80")?;
// accept connections and process them serially
for stream in listener.incoming() {
handle_client(stream?);
}
Ok(())
}