-use tokio::net::TcpListener;
+use tokio::{
+ io::{AsyncReadExt, AsyncWriteExt},
+ net::TcpListener,
+};
// TODO: write an echo server that accepts incoming TCP connections and
// echoes the received data back to the client.
// - `tokio::net::TcpStream::split` to obtain a reader and a writer from the socket
// - `tokio::io::copy` to copy data from the reader to the writer
pub async fn echo(listener: TcpListener) -> Result<(), anyhow::Error> {
- todo!()
+ loop {
+ let (mut tcp_stream, _socket_addr) = listener.accept().await?;
+ let (mut reader, mut writer) = tcp_stream.split();
+ tokio::io::copy(&mut reader, &mut writer).await?;
+ }
}
#[cfg(test)]