Initial commit

This commit is contained in:
椰子 2022-06-04 01:55:01 +08:00
commit 6a02133b7b
3 changed files with 32 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "echo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

23
src/bin/main.rs Normal file
View File

@ -0,0 +1,23 @@
use std::net::{TcpListener, TcpStream};
use std::io::prelude::*;
fn main() {
let listener = TcpListener::bind("0.0.0.0:8888").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
}
}
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 1024];
let remote_addr = stream.peer_addr().unwrap().ip().to_string();
stream.read(&mut buffer).unwrap();
let response = "HTTP/1.1 200 OK\r\n\r\n";
stream.write(response.as_bytes()).unwrap();
stream.write(remote_addr.as_bytes()).unwrap();
stream.flush().unwrap();
}