1//! A "hello world" echo server with Tokio
2//!
3//! This server will create a TCP listener, accept connections in a loop, and
4//! write back everything that's read off of each TCP connection.
5//!
6//! Because the Tokio runtime uses a thread pool, each TCP connection is
7//! processed concurrently with all other TCP connections across multiple
8//! threads.
9//!
10//! To see this server in action, you can run this in one terminal:
11//!
12//! cargo run --example echo
13//!
14//! and in another terminal you can run:
15//!
16//! cargo run --example connect 127.0.0.1:8080
17//!
18//! Each line you type in to the `connect` terminal should be echo'd back to
19//! you! If you open up multiple terminals running the `connect` example you
20//! should be able to see them all make progress simultaneously.
21
22#![warn(rust_2018_idioms)]
23
24use tokio::io::{AsyncReadExt, AsyncWriteExt};
25use tokio::net::TcpListener;
26
27use std::env;
28use std::error::Error;
29
30#[tokio::main]
31async fn main() -> Result<(), Box<dyn Error>> {
32 // Allow passing an address to listen on as the first argument of this
33 // program, but otherwise we'll just set up our TCP listener on
34 // 127.0.0.1:8080 for connections.
35 let addr = env::args()
36 .nth(1)
37 .unwrap_or_else(|| "127.0.0.1:8080".to_string());
38
39 // Next up we create a TCP listener which will listen for incoming
40 // connections. This TCP listener is bound to the address we determined
41 // above and must be associated with an event loop.
42 let listener = TcpListener::bind(&addr).await?;
43 println!("Listening on: {}", addr);
44
45 loop {
46 // Asynchronously wait for an inbound socket.
47 let (mut socket, _) = listener.accept().await?;
48
49 // And this is where much of the magic of this server happens. We
50 // crucially want all clients to make progress concurrently, rather than
51 // blocking one on completion of another. To achieve this we use the
52 // `tokio::spawn` function to execute the work in the background.
53 //
54 // Essentially here we're executing a new task to run concurrently,
55 // which will allow all of our clients to be processed concurrently.
56
57 tokio::spawn(async move {
58 let mut buf = vec![0; 1024];
59
60 // In a loop, read data from the socket and write the data back.
61 loop {
62 let n = socket
63 .read(&mut buf)
64 .await
65 .expect("failed to read data from socket");
66
67 if n == 0 {
68 return;
69 }
70
71 socket
72 .write_all(&buf[0..n])
73 .await
74 .expect("failed to write data to socket");
75 }
76 });
77 }
78}
79