Cannot read data from TcpStream


I am having some trouble with the std::net::TcpStream struct…
I set up an echo server using ncat on my machine which is listening on port 12345:
ncat -l 12345 -e /bin/cat -k

I try to send some data to this server using a little rust program, which should then read the servers response and print it to stdout:

use std::net::{SocketAddr, TcpStream};
use std::io::{self, Read, Write};

fn send_recv(addr: &str, data: &str) -> io::Result<String> {
    let mut sock = try!(TcpStream::connect(addr));
    println!("Data was sent...");

    let mut buf = String::new();
    try!(sock.read_to_string(&mut buf));

fn main() {
    match send_recv("localhost:12345", "Hello World!") {
        Ok(r)  => println!("success: {}", r),
        Err(e) => println!("error: {}", e)

The data is sent to the server, but my program won’t ever read the servers response, and I’m not sure why that is.
Any help would be appreciated!


Nothing indicates to cat that the stream is finished, so read_to_string is going to wait forever on more data.


Yes, I just figured that out too…
Now thats kind of emberassing :sweat:

However, thanks for pointing it out @sfackler :slight_smile: