Initial commit
This commit is contained in:
commit
6a02133b7b
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal 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
23
src/bin/main.rs
Normal 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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user