[−][src]Struct romio::TcpListener
A TCP socket server, listening for connections.
After creating a TcpListener
by bind
ing it to a socket address, it listens
for incoming TCP connections. These can be accepted by awaiting elements from the
async stream of incoming connections, incoming
.
The socket will be closed when the value is dropped.
Examples
#![feature(async_await, await_macro, futures_api)] use std::error::Error; use romio::tcp::{TcpListener, TcpStream}; use futures::prelude::*; async fn recite_shakespeare(mut stream: TcpStream) { await!(stream.write_all(b"Shall I hear more, or shall I speak at this?")); } async fn listen() -> Result<(), Box<dyn Error + 'static>> { let socket_addr = "127.0.0.1:80".parse()?; let mut listener = TcpListener::bind(&socket_addr)?; let mut incoming = listener.incoming(); // accept connections and process them serially while let Some(stream) = await!(incoming.next()) { await!(recite_shakespeare(stream?)); } Ok(()) }
Methods
impl TcpListener
[src]
pub fn bind(addr: &SocketAddr) -> Result<TcpListener>
[src]
Creates a new TcpListener
which will be bound to the specified
address.
The returned listener is ready for accepting connections.
Binding with a port number of 0 will request that the OS assigns a port
to this listener. The port allocated can be queried via the
local_addr
method.
Examples
Create a TCP listener bound to 127.0.0.1:80:
use romio::tcp::TcpListener; let socket_addr = "127.0.0.1:80".parse()?; let listener = TcpListener::bind(&socket_addr)?;
pub fn local_addr(&self) -> Result<SocketAddr>
[src]
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 romio::tcp::TcpListener; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; let socket_addr = "127.0.0.1:8080".parse()?; let listener = TcpListener::bind(&socket_addr)?; let expected = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); assert_eq!(listener.local_addr()?, SocketAddr::V4(expected));
pub fn incoming(&mut self) -> Incoming
[src]
Consumes this listener, returning a stream of the sockets this listener accepts.
This method returns an implementation of the Stream
trait which
resolves to the sockets the are accepted on this listener.
Errors
Note that accepting a connection can lead to various errors and not all of them are necessarily fatal ‒ for example having too many open file descriptors or the other side closing the connection while it waits in an accept queue. These would terminate the stream if not handled in any way.
Examples
#![feature(async_await, await_macro, futures_api)] use futures::prelude::*; use romio::tcp::TcpListener; let socket_addr = "127.0.0.1:80".parse()?; let mut listener = TcpListener::bind(&socket_addr)?; let mut incoming = listener.incoming(); // accept connections and process them serially while let Some(stream) = await!(incoming.next()) { match stream { Ok(stream) => { println!("new client!"); }, Err(e) => { /* connection failed */ } } }
pub fn ttl(&self) -> Result<u32>
[src]
Gets the value of the IP_TTL
option for this socket.
For more information about this option, see set_ttl
.
Examples
#![feature(async_await, await_macro, futures_api)] use futures::prelude::*; use romio::tcp::TcpListener; let socket_addr = "127.0.0.1:0".parse()?; let listener = TcpListener::bind(&socket_addr)?; listener.set_ttl(100)?; assert_eq!(listener.ttl()?, 100);
pub fn set_ttl(&self, ttl: u32) -> Result<()>
[src]
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
#![feature(async_await, await_macro, futures_api)] use futures::prelude::*; use romio::tcp::TcpListener; let socket_addr = "127.0.0.1:0".parse()?; let listener = TcpListener::bind(&socket_addr)?; listener.set_ttl(100)?;
Trait Implementations
impl Debug for TcpListener
[src]
impl AsRawFd for TcpListener
[src]
impl AsyncReady for TcpListener
[src]
type Ok = (TcpStream, SocketAddr)
The type of successful values yielded by this trait.
type Err = Error
The type of failures yielded by this trait.
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<Self::Ok, Self::Err>>
[src]
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<Self::Ok, Self::Err>>
Check if the stream can be read from.
default fn poll_ready_unpin(
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<Self::Ok, Self::Err>> where
Self: Unpin,
[src]
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<Self::Ok, Self::Err>> where
Self: Unpin,
A convenience for calling AsyncReady::poll_ready
on Unpin
types.
Auto Trait Implementations
impl Send for TcpListener
impl Sync for TcpListener
Blanket Implementations
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,