Skip to main content

TcpListener

Struct TcpListener 

Source
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

Source

pub fn bind<A: ToSocketAddrs>(addr: A) -> Result<TcpListener, NetError>

Binds with safe defaults (see crate::listen).

§Errors

AddrInUse, PermissionDenied, AddrNotAvailable, NoAddresses, Bind.

Source

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.

Source

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.

Source

pub fn accept_timeout( &self, timeout: Duration, ) -> Result<Option<(TcpConnection, ConnectionInfo)>, NetError>

As TcpListener::accept, but Ok(None) when nothing arrived within timeout.

§Errors

As TcpListener::accept.

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());
Source

pub fn info(&self) -> &ListenerInfo

Facts about this listener.

Source

pub fn local_addr(&self) -> Result<SocketAddr, NetError>

The bound address.

§Errors

Os when getsockname fails.

Source

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());
Source

pub fn into_std(self) -> TcpListener

Gives the socket up (it stays non-blocking).

Source

pub fn try_clone(&self) -> Result<TcpListener, NetError>

A second handle to the same listening socket.

§Errors

Os when duplication fails.

Trait Implementations§

Source§

impl AsFd for TcpListener

Available on Unix only.
Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
Source§

impl Debug for TcpListener

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<TcpListener> for OwnedFd

Available on Unix only.
Source§

fn from(l: TcpListener) -> OwnedFd

Converts to this type from the input type.
Source§

impl TryFrom<OwnedFd> for TcpListener

Available on Unix only.
Source§

type Error = NetError

The type returned in the event of a conversion error.
Source§

fn try_from(fd: OwnedFd) -> Result<TcpListener, NetError>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.