mirror of https://github.com/xvxx/phd
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.
43 lines
1.0 KiB
Rust
43 lines
1.0 KiB
Rust
#![allow(unused_must_use)]
|
|
|
|
extern crate async_std;
|
|
|
|
use async_std::{
|
|
io::BufReader,
|
|
net::{TcpListener, TcpStream, ToSocketAddrs},
|
|
prelude::*,
|
|
task,
|
|
};
|
|
|
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
|
|
|
fn main() {
|
|
println!("-> Listening on localhost:7070");
|
|
let fut = listen("localhost:7070");
|
|
if let Err(e) = task::block_on(fut) {
|
|
eprintln!("{}", e);
|
|
}
|
|
}
|
|
|
|
async fn listen(addr: impl ToSocketAddrs) -> Result<()> {
|
|
let listener = TcpListener::bind(addr).await?;
|
|
let mut incoming = listener.incoming();
|
|
while let Some(stream) = incoming.next().await {
|
|
let stream = stream?;
|
|
println!("-> Connection from: {}", stream.peer_addr()?);
|
|
task::spawn(client_loop(stream));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
async fn client_loop(stream: TcpStream) -> Result<()> {
|
|
let reader = BufReader::new(&stream);
|
|
let mut lines = reader.lines();
|
|
|
|
while let Some(line) = lines.next().await {
|
|
let line = line?;
|
|
println!("-> client sent: {:?}", line);
|
|
}
|
|
Ok(())
|
|
}
|