Browse Source

test example for server that replies request by line

Key-exchange
Andrew Onuchowski 2 years ago
parent
commit
42f5ebd7bc
1 changed files with 29 additions and 1 deletions
  1. 29
    1
      src/bin/server.rs

+ 29
- 1
src/bin/server.rs View File

use std::{
io::{prelude::*, BufReader},
net::{TcpListener, TcpStream},
};
use std::io::Lines;

fn main() { fn main() {
println! ("server");
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();

for stream in listener.incoming() {
let stream = stream.unwrap();

handle_connection(stream);
}
}

fn handle_connection(mut stream: TcpStream) {
let mut stream2: TcpStream = stream.try_clone().unwrap();
let buf_reader: BufReader<&mut TcpStream> = BufReader::new(&mut stream);
let request: Lines<BufReader<&mut TcpStream>> = buf_reader.lines();

println!("Request: {:#?}", request);

for elem in request
{
let s = elem.unwrap();
println!("{:?}", s);
stream2.write(s.as_bytes()).unwrap();
stream2.write_all(b"\n").unwrap();
}
} }

Loading…
Cancel
Save