mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-16 18:59:55 +00:00
1d875836a2
* Implement Clone for TcpStream * Update examples * Remove accidentally added examples
39 lines
909 B
Rust
39 lines
909 B
Rust
//! TCP echo server.
|
|
//!
|
|
//! To send messages, do:
|
|
//!
|
|
//! ```sh
|
|
//! $ nc localhost 8080
|
|
//! ```
|
|
|
|
use async_std::io;
|
|
use async_std::net::{TcpListener, TcpStream};
|
|
use async_std::prelude::*;
|
|
use async_std::task;
|
|
|
|
async fn process(stream: TcpStream) -> io::Result<()> {
|
|
println!("Accepted from: {}", stream.peer_addr()?);
|
|
|
|
let mut reader = stream.clone();
|
|
let mut writer = stream;
|
|
io::copy(&mut reader, &mut writer).await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn main() -> io::Result<()> {
|
|
task::block_on(async {
|
|
let listener = TcpListener::bind("127.0.0.1:8080").await?;
|
|
println!("Listening on {}", listener.local_addr()?);
|
|
|
|
let mut incoming = listener.incoming();
|
|
|
|
while let Some(stream) = incoming.next().await {
|
|
let stream = stream?;
|
|
task::spawn(async {
|
|
process(stream).await.unwrap();
|
|
});
|
|
}
|
|
Ok(())
|
|
})
|
|
}
|