EDIT: It is now solved, the problem was some kind of strange behaviour in Windows with the way I was using to connect to the server. Sorry :(
I am trying to write a simple TCP server in which each connection is processed in a new thread. The connection must be kept active for a long period of time, and while client's connections are being processed, new connections must be accepted and processed.
Here an example of the code I am running:
use std::net::{TcpListener, TcpStream};
use std::io::{Write, Read};
fn main() {
let addr_str = format!("0.0.0.0:{}", 1234);
if let Ok(tcp_listener) = std::net::TcpListener::bind(addr_str) {
tcp_listener.set_nonblocking(true);
println!("listening on 0.0.0.0:{}", 1234);
for tcp_stream in tcp_listener.incoming() {
if let Ok(mut tstream) = tcp_stream {
tstream.set_nonblocking(true);
println!("open connection!");
std::thread::spawn(move|| {
std::thread::sleep(std::time::Duration::from_millis(10000)); // sleep to simulate the processing, but in the real server it cloud be a really long task.. probably hours..
tstream.write(b"hello world:)");
println!("client disconnected ...");
});
}
}
} else {
println!("ERROR: unable to create TcpListener");
}
}
The problem is it only accepts 2 connections at the same time, so no new connections are accepted and processed until one of the previously accepted connection is finished.
Please, could you tell me what I am doing wrong? Thank you very much!