Created
July 26, 2021 15:33
-
-
Save dmfutcher/e0a913428b34b2727474ace3e741c62c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Super simple TCP echo server using Tokio | |
// basically a couple tutorials spliced together by DM Futcher (github.com/dmfutcher) | |
use tokio::net::{TcpListener, TcpStream}; | |
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; | |
async fn process_connection(mut stream : TcpStream) { | |
let (reader, mut writer) = stream.split(); | |
let buf = BufReader::new(reader); | |
let mut lines = buf.lines(); | |
while let Some(line) = lines.next_line().await.unwrap() { | |
println!("{}", line); | |
writer.write(line.as_bytes()).await.unwrap(); | |
} | |
} | |
#[tokio::main] | |
pub async fn main() { | |
let listener = TcpListener::bind("127.0.0.1:8099").await.unwrap(); | |
loop { | |
let (socket, _) = listener.accept().await.unwrap(); | |
tokio::spawn(async move { | |
process_connection(socket).await; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment