pub struct Listener(_);Expand description
It is used to accept incoming HTTP/2 connections.
Implementations§
Methods from Deref<Target = TlsListener>§
pub fn accept_tls( &self ) -> impl Future<Output = Result<(TlsStream<TcpStream>, SocketAddr), Error>>
pub async fn accept_tls_with<F>( &self, f: F ) -> impl Future<Output = Result<(TlsStream<TcpStream>, SocketAddr), Error>>where F: FnOnce(&mut ServerConnection),
Methods from Deref<Target = TcpListener>§
sourcepub async fn accept(
&self
) -> impl Future<Output = Result<(TcpStream, SocketAddr), Error>>
pub async fn accept( &self ) -> impl Future<Output = Result<(TcpStream, SocketAddr), Error>>
Accepts a new incoming connection from this listener.
This function will yield once a new TCP connection is established. When
established, the corresponding TcpStream and the remote peer’s
address will be returned.
Cancel safety
This method is cancel safe. If the method is used as the event in a
tokio::select! statement and some other branch
completes first, then it is guaranteed that no new connections were
accepted by this method.
Examples
use tokio::net::TcpListener;
use std::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
match listener.accept().await {
Ok((_socket, addr)) => println!("new client: {:?}", addr),
Err(e) => println!("couldn't get client: {:?}", e),
}
Ok(())
}sourcepub fn poll_accept(
&self,
cx: &mut Context<'_>
) -> Poll<Result<(TcpStream, SocketAddr), Error>>
pub fn poll_accept( &self, cx: &mut Context<'_> ) -> Poll<Result<(TcpStream, SocketAddr), Error>>
Polls to accept a new incoming connection to this listener.
If there is no connection to accept, Poll::Pending is returned and the
current task will be notified by a waker. Note that on multiple calls
to poll_accept, only the Waker from the Context passed to the most
recent call is scheduled to receive a wakeup.
sourcepub fn local_addr(&self) -> Result<SocketAddr, Error>
pub fn local_addr(&self) -> Result<SocketAddr, Error>
Returns the local address that this listener is bound to.
This can be useful, for example, when binding to port 0 to figure out which port was actually bound.
Examples
use tokio::net::TcpListener;
use std::io;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
assert_eq!(listener.local_addr()?,
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
Ok(())
}sourcepub fn ttl(&self) -> Result<u32, Error>
pub fn ttl(&self) -> Result<u32, Error>
Gets the value of the IP_TTL option for this socket.
For more information about this option, see set_ttl.
Examples
use tokio::net::TcpListener;
use std::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:0").await?;
listener.set_ttl(100).expect("could not set TTL");
assert_eq!(listener.ttl()?, 100);
Ok(())
}sourcepub fn set_ttl(&self, ttl: u32) -> Result<(), Error>
pub fn set_ttl(&self, ttl: u32) -> Result<(), Error>
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.
Examples
use tokio::net::TcpListener;
use std::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:0").await?;
listener.set_ttl(100).expect("could not set TTL");
Ok(())
}