Skip to main content

TcpListener

Struct TcpListener 

Source
pub struct TcpListener { /* private fields */ }
Expand description

Async TCP listening socket.

A listener accepts inbound TcpStream connections from a bound local address. Use TcpListener::accept for one connection at a time or TcpListener::incoming for a Stream adapter.

Implementations§

Source§

impl TcpListener

Source

pub fn from_std(listener: TcpListener) -> Result<Self>

Available on Unix only.

Adopts a blocking std::net::TcpListener, returning an async TcpListener.

The socket is switched to non-blocking mode. Ownership of the descriptor transfers to the returned listener.

Source§

impl TcpListener

Source

pub async fn bind<A>(addr: A) -> Result<Self>
where A: ToSocketAddrs + Send + 'static,

Binds a TCP listener to the first resolved address that succeeds.

Binding to port 0 asks the OS to assign an available port, which can be retrieved with local_addr.

Like std::net::TcpListener::bind, this does not set SO_REUSEADDR. To reuse a recently-closed address (or bind while a previous socket is in TIME_WAIT), configure a TcpSocket and enable it before binding:

runite::spawn(async {
    let socket = runite::net::TcpSocket::new_v4().unwrap();
    socket.set_reuseaddr(true).unwrap();
    socket.bind("127.0.0.1:0".parse().unwrap()).unwrap();
    let listener = socket.listen(1024).unwrap();
    assert!(listener.local_addr().unwrap().port() != 0);
});
runite::run();
§Examples
runite::spawn(async {
    let listener = runite::net::TcpListener::bind("127.0.0.1:0")
        .await
        .unwrap();
    assert!(listener.local_addr().unwrap().port() != 0);
});
runite::run();
Source

pub async fn accept(&self) -> Result<(TcpStream, SocketAddr)>

Accepts an incoming connection.

The returned address is the peer address reported by the operating system for the accepted stream.

Source

pub fn incoming(&self) -> Incoming

Returns a Stream that yields inbound connections as they arrive.

The stream is infinite: it never yields None. Each item is the result of an accept, so transient errors surface as Some(Err(_)) without ending iteration.

Source

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

Returns the local socket address of this listener.

Source

pub fn ttl(&self) -> Result<u32>

Reads the listener socket’s IP time-to-live value.

Source

pub fn set_ttl(&self, ttl: u32) -> Result<()>

Sets the listener socket’s IP time-to-live value.

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 AsRawFd for TcpListener

Available on Unix only.
Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw 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<OwnedFd> for TcpListener

Available on Unix only.
Source§

fn from(fd: OwnedFd) -> Self

Adopts an already-listening TCP socket. Use TcpListener::from_std to adopt a std::net::TcpListener and set the expected mode.

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more