I seem to be scared with std :: io :: TcpStream. I am actually trying to open a TCP connection to another system, but the code below exactly mimics the problem.
I have a Tcp server that simply writes “Hello World” to TcpStream when opened, and then loops to open the connection.
fn main() { let listener = io::TcpListener::bind("127.0.0.1", 8080); let mut acceptor = listener.listen(); for stream in acceptor.incoming() { match stream { Err(_) => { } Ok(stream) => spawn(proc() { handle(stream); }) } } drop(acceptor); } fn handle(mut stream: io::TcpStream) { stream.write(b"Hello Connection"); loop {} }
All the client does is try to read one byte from the connection and print it.
fn main() { let mut socket = io::TcpStream::connect("127.0.0.1", 8080).unwrap(); loop { match socket.read_byte() { Ok(i) => print!("{}", i), Err(e) => { println!("Error: {}", e); break } } } }
Now the problem is that my client remains blocked while reading until I kill the server or close the TCP connection. This is not what I want, I need to open a TCP connection for a very long time and send messages back and forth between the client and server. What i don't understand here? I have the same problem with the real system that I am talking to - I will only unlock as soon as I kill the connection.
source share