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));
try!(sock.write_all(data.as_bytes()));
println!("Data was sent...");
let mut buf = String::new();
try!(sock.read_to_string(&mut buf));
Ok(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!