pub struct TcpListener { /* private fields */ }Expand description
A listening TCP socket created by net-io, so options that must precede bind can be set.
§Examples
let listener = net_io::listen("0.0.0.0:7000")?;
loop {
let (mut conn, info) = listener.accept()?;
println!("connection {} from {:?}", info.id, info.client_addr());
conn.send_all(b"hello\n")?;
conn.close()?;
}Implementations§
Source§impl TcpListener
impl TcpListener
Sourcepub fn bind<A: ToSocketAddrs>(addr: A) -> Result<TcpListener, NetError>
pub fn bind<A: ToSocketAddrs>(addr: A) -> Result<TcpListener, NetError>
Binds with safe defaults (see crate::listen).
§Errors
AddrInUse, PermissionDenied, AddrNotAvailable, NoAddresses, Bind.
Sourcepub fn bind_with<A: ToSocketAddrs>(
addr: A,
options: ListenOptions,
) -> Result<TcpListener, NetError>
pub fn bind_with<A: ToSocketAddrs>( addr: A, options: ListenOptions, ) -> Result<TcpListener, NetError>
Binds with options, applied in the order docs/API.md gives.
§Errors
As TcpListener::bind; Unsupported for an option the platform lacks.
Sourcepub fn accept(&self) -> Result<(TcpConnection, ConnectionInfo), NetError>
pub fn accept(&self) -> Result<(TcpConnection, ConnectionInfo), NetError>
Blocks until a connection arrives; applies per-connection options, normalizes
the peer, issues a ConnectionId. No admission: a plain listener has no gate.
§Errors
Limit { which: Descriptors } when exhaustion persists past one backoff step; Os.
Sourcepub fn accept_timeout(
&self,
timeout: Duration,
) -> Result<Option<(TcpConnection, ConnectionInfo)>, NetError>
pub fn accept_timeout( &self, timeout: Duration, ) -> Result<Option<(TcpConnection, ConnectionInfo)>, NetError>
As TcpListener::accept, but Ok(None) when nothing arrived within timeout.
§Errors
let listener = net_io::listen("127.0.0.1:0")?;
let got = listener.accept_timeout(std::time::Duration::from_millis(10))?;
assert!(got.is_none());Sourcepub fn info(&self) -> &ListenerInfo
pub fn info(&self) -> &ListenerInfo
Facts about this listener.
Sourcepub fn local_addr(&self) -> Result<SocketAddr, NetError>
pub fn local_addr(&self) -> Result<SocketAddr, NetError>
Sourcepub fn from_std(listener: TcpListener) -> Result<TcpListener, NetError>
pub fn from_std(listener: TcpListener) -> Result<TcpListener, NetError>
Adopts an existing listening socket (checked with SO_ACCEPTCONN and SO_TYPE).
§Errors
InvalidConfig { field: "socket", reason: NotListening }.
let std_listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let listener = net_io::TcpListener::from_std(std_listener)?;
assert!(listener.local_addr().is_ok());Sourcepub fn into_std(self) -> TcpListener
pub fn into_std(self) -> TcpListener
Gives the socket up (it stays non-blocking).