Struct std::net::unix::TcpListener
A TCP listener which can accept new connections.
Fields
-
socket: Socket
Methods
impl TcpListener { ... }
-
fn from_socket(socket: Socket) -> TcpListener
Create a new TCP listener from a socket.
-
fn bind(addr: &SocketAddr) -> Result<TcpListener>
Creates a new TCP listener bound to the specified address.
Example
use std::net::{TcpListener, SocketAddr}; let addr = SocketAddr::parse("[::]:8080").unwrap(); let listener = TcpListener::bind(&addr).unwrap(); defer listener.close(); loop { let (stream, peer) = listener.accept().unwrap(); defer stream.close(); println!("Accepted connection from {}", peer); }Run this example -
fn as_fd(self: &TcpListener) -> FileDescriptor
-
fn accept(self: &TcpListener) -> Result<(TcpStream, SocketAddr)>
Accepts a new incoming connection.
Returns a new
TcpStreamrepresenting the accepted connection and the address of the remote peer. -
fn socket_addr(self: &TcpListener) -> Result<SocketAddr>
Returns the address of the remote peer
-
fn set_ttl(self: &TcpListener, ttl: u32) -> Result<()>
Sets the time-to-live (TTL) value for the socket.
This value controls the number of hops (routers) that packets can traverse before being discarded. Works for both IPv4 and IPv6 sockets.
Example
use std::net::{TcpListener, SocketAddr}; let addr = SocketAddr::parse("[::]:8080").unwrap(); let listener = TcpListener::bind(&addr).unwrap(); defer listener.close(); listener.set_ttl(64).unwrap(); assert_eq!(listener.ttl().unwrap(), 64);Run this example -
fn ttl(self: &TcpListener) -> Result<u32>
Gets the time-to-live (TTL) value for the socket.
Works for both IPv4 and IPv6 sockets.
Example
use std::net::{TcpListener, SocketAddr}; let addr = SocketAddr::parse("[::]:8080").unwrap(); let listener = TcpListener::bind(&addr).unwrap(); defer listener.close(); let ttl = listener.ttl().unwrap(); println!("TTL: {}", ttl);Run this example -
fn set_nonblocking(self: &mut TcpListener, nonblocking: bool) -> Result<()>
Sets the socket to non-blocking or blocking mode.
In non-blocking mode, accept operations will return immediately with an error if they would block.
Example
use std::net::{TcpListener, SocketAddr}; let addr = SocketAddr::parse("[::]:8080").unwrap(); let listener = TcpListener::bind(&addr).unwrap(); defer listener.close(); listener.set_nonblocking(true).unwrap();Run this example -
fn close(self: &mut TcpListener) -> Result<()>
Closes the socket.
-
fn move(self: &mut TcpListener) -> TcpStream