pub struct TcpListener(_);
A structure representing a socket server.
use std::net::{TcpListener, TcpStream};
let listener = TcpListener::bind("127.0.0.1:80").unwrap();
fn handle_client(stream: TcpStream) {
// ...
}
// accept connections and process them serially
for stream in listener.incoming() {
match stream {
Ok(stream) => {
handle_client(stream);
}
Err(e) => { /* connection failed */ }
}
} impl TcpListener
[src]
fn bind<A: ToSocketAddrs>(addr: A) -> Result<TcpListener>Creates a new TcpListener which will be bound to the specified address.
The returned listener is ready for accepting connections.
Binding with a port number of 0 will request that the OS assigns a port to this listener. The port allocated can be queried via the local_addr method.
The address type can be any implementor of ToSocketAddrs trait. See its documentation for concrete examples.
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap(); fn local_addr(&self) -> Result<SocketAddr>Returns the local socket address of this listener.
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener};
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
assert_eq!(listener.local_addr().unwrap(),
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080))); fn try_clone(&self) -> Result<TcpListener>Creates a new independently owned handle to the underlying socket.
The returned TcpListener is a reference to the same socket that this object references. Both handles can be used to accept incoming connections and options set on one listener will affect the other.
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
let listener_clone = listener.try_clone().unwrap(); fn accept(&self) -> Result<(TcpStream, SocketAddr)>Accept a new incoming connection from this listener.
This function will block the calling thread until a new TCP connection is established. When established, the corresponding TcpStream and the remote peer's address will be returned.
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
match listener.accept() {
Ok((_socket, addr)) => println!("new client: {:?}", addr),
Err(e) => println!("couldn't get client: {:?}", e),
} fn incoming(&self) -> IncomingReturns an iterator over the connections being received on this listener.
The returned iterator will never return None and will also not yield the peer's SocketAddr structure.
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap();
for stream in listener.incoming() {
match stream {
Ok(stream) => {
println!("new client!");
}
Err(e) => { /* connection failed */ }
}
} fn set_ttl(&self, ttl: u32) -> Result<()>Sets the value for the IP_TTL option on this socket.
This value sets the time-to-live field that is used in every packet sent from this socket.
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap();
listener.set_ttl(100).expect("could not set TTL"); fn ttl(&self) -> Result<u32>Gets the value of the IP_TTL option for this socket.
For more information about this option, see set_ttl().
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap();
listener.set_ttl(100).expect("could not set TTL");
assert_eq!(listener.ttl().unwrap_or(0), 100); fn set_only_v6(&self, only_v6: bool) -> Result<()>fn only_v6(&self) -> Result<bool>fn take_error(&self) -> Result<Option<Error>>Get the value of the SO_ERROR option on this socket.
This will retrieve the stored error in the underlying socket, clearing the field in the process. This can be useful for checking errors between calls.
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap();
listener.take_error().expect("No error was expected"); fn set_nonblocking(&self, nonblocking: bool) -> Result<()>Moves this TCP stream into or out of nonblocking mode.
On Unix this corresponds to calling fcntl, and on Windows this corresponds to calling ioctlsocket.
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:80").unwrap();
listener.set_nonblocking(true).expect("Cannot set non-blocking"); impl Debug for TcpListener
[src]
fn fmt(&self, f: &mut Formatter) -> ResultFormats the value using the given formatter.
impl AsRawFd for TcpListener
[src]
fn as_raw_fd(&self) -> RawFdExtracts the raw file descriptor. Read more
impl FromRawFd for TcpListener
unsafe fn from_raw_fd(fd: RawFd) -> TcpListenerConstructs a new instance of Self from the given raw file descriptor. Read more
impl IntoRawFd for TcpListener
fn into_raw_fd(self) -> RawFdConsumes this object, returning the raw underlying file descriptor. Read more
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/net/struct.TcpListener.html