futures_net/tcp/
mod.rs

1//! Async TCP.
2//!
3//! This module contains the TCP networking types, similar to those found in
4//! `std::net`, but suitable for async programming via futures and
5//! `async`/`await`.
6//!
7//! - To connect to an address via TCP, use [`TcpStream::connect`].
8//! - To listen for TCP connection, use [`TcpListener::bind`] and then
9//!   [`TcpListener::incoming`].
10//! - Once you have a [`TcpStream`], you can use methods from `AsyncRead`,
11//!   `AsyncWrite`, and their extension traits (`AsyncReadExt`, `AsyncWriteExt`)
12//!   to send and receive data.
13//!
14//! [`TcpStream`]: struct.TcpStream.html
15//! [`TcpStream::connect`]: struct.TcpStream.html#method.connect
16//! [`TcpListener::bind`]: struct.TcpListener.html#method.bind
17//! [`TcpListener::incoming`]: struct.TcpListener.html#method.incoming
18//!
19//! # Example
20//!
21//! ```no_run
22//! use futures_net::{TcpListener, TcpStream};
23//! use futures::prelude::*;
24//!
25//! async fn say_hello(mut stream: TcpStream) {
26//!     stream.write_all(b"Shall I hear more, or shall I speak at this?!").await;
27//! }
28//!
29//! async fn listen() -> Result<(), Box<dyn std::error::Error + 'static>> {
30//!     let socket_addr = "127.0.0.1:80".parse()?;
31//!     let mut listener = TcpListener::bind(&socket_addr)?;
32//!     let mut incoming = listener.incoming();
33//!
34//!     // accept connections and process them serially
35//!     while let Some(stream) = incoming.next().await {
36//!         say_hello(stream?).await;
37//!     }
38//!     Ok(())
39//! }
40//! ```
41
42mod listener;
43mod stream;
44
45pub use self::listener::{Incoming, TcpListener};
46pub use self::stream::{ConnectFuture, TcpStream};