Add files via upload

This commit is contained in:
Dangrain 2024-12-18 22:24:43 +01:00 committed by GitHub
parent 4adc7e1651
commit ad204d06e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,42 @@
use std::io::Write;
use std::net::{TcpListener, TcpStream};
use std::thread;
use std::time::Duration;
fn handle_client(mut stream: TcpStream) {
let mut counter = 0;
loop {
let variables = [counter, counter + 1, counter + 2];
let serialized_data = format!("{:?}\n", variables);
if let Err(e) = stream.write_all(serialized_data.as_bytes()) {
eprintln!("Failed to send data: {}", e);
break;
}
println!("Sent: {:?}", variables);
counter += 1;
thread::sleep(Duration::from_secs(1)); // Delay to avoid spamming
}
}
fn main() -> std::io::Result<()> {
let listener = TcpListener::bind("0.0.0.0:4000")?;
println!("Server listening on port 4000");
for stream in listener.incoming() {
match stream {
Ok(stream) => {
println!("New connection: {}", stream.peer_addr().unwrap());
thread::spawn(|| handle_client(stream));
}
Err(e) => {
eprintln!("Connection failed: {}", e);
}
}
}
Ok(())
}