You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
883 B
Rust
39 lines
883 B
Rust
//! TCP echo server.
|
|
//!
|
|
//! To send messages, do:
|
|
//!
|
|
//! ```sh
|
|
//! $ nc localhost 8080
|
|
//! ```
|
|
|
|
#![feature(async_await)]
|
|
|
|
use async_std::net::{TcpListener, TcpStream};
|
|
use async_std::{io, prelude::*, task};
|
|
|
|
async fn process(stream: TcpStream) -> io::Result<()> {
|
|
println!("Accepted from: {}", stream.peer_addr()?);
|
|
|
|
let (reader, writer) = &mut (&stream, &stream);
|
|
io::copy(reader, 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(())
|
|
})
|
|
}
|