Skip to content

Instantly share code, notes, and snippets.

@FauxFaux
Created September 21, 2024 15:53
Show Gist options
  • Save FauxFaux/6e8102b514360d01fce59fffa61c01a7 to your computer and use it in GitHub Desktop.
Save FauxFaux/6e8102b514360d01fce59fffa61c01a7 to your computer and use it in GitHub Desktop.
use anyhow::Result;
use std::net::SocketAddr;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::{TcpListener, TcpStream};
async fn echo(mut socket: TcpStream, addr: SocketAddr) -> Result<()> {
println!("Incoming connection from: {}", addr);
let (read, mut write) = socket.split();
let mut read = BufReader::new(read);
write.write_all(b"Welcome!").await?;
loop {
let mut line = String::new();
let len = read.read_line(&mut line).await?;
if len == 0 {
println!("Connection closed");
break;
}
if line.trim().to_lowercase() == "exit" {
println!("Exiting");
break;
}
write.write_all(line.as_bytes()).await?;
println!("Line: {}", line);
}
Ok(())
}
#[tokio::main]
async fn main() -> Result<()> {
let listener = TcpListener::bind("localhost:16000").await?;
loop {
let (mut stream, addr) = listener.accept().await?;
tokio::spawn(async move { echo(stream, addr).await });
}
println!("Hello, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment