use std::net::{TcpListener, TcpStream};
use std::io::{Read, Write};
fn handle_client(mut stream: TcpStream) {
let mut buffer = [0; 1024];
let host = hostname::get();
let host = match host {
Ok(oss) => {
let s = oss.into_string();
match s {
Ok(st) => st,
Err(_) => String::from("unknown1")
}
},
Err(_) => String::from("unknown2")
};
let host = host.as_bytes();
// Read data from the client
stream.write_all(&host).unwrap();
/*
match stream.read(&mut buffer) {
Ok(size) => {
// Echo back the received data
stream.write_all(&host).unwrap();
}
Err(e) => {
eprintln!("Error reading from the client: {}", e);
}
}
*/
}
fn main() {
// Bind to a specific address and port
let listener = TcpListener::bind("0.0.0.0:1026").expect("Failed to bind to address");
println!("Server listening on 0.0.0.0:1026");
// Accept incoming connections and process them in a loop
for stream in listener.incoming() {
match stream {
Ok(stream) => {
// Spawn a new thread to handle each incoming connection
std::thread::spawn(|| {
handle_client(stream);
});
}
Err(e) => {
eprintln!("Error accepting connection: {}", e);
}
}
}
}