I am trying to follow the Tokio client tutorial to write a client that talks to an echo server that sends back the response with a newline at the end. Here is what I have:
extern crate futures;
extern crate tokio_core;
extern crate tokio_io;
use std::net::SocketAddr;
use std::io::BufReader;
use futures::Future;
use tokio_core::reactor::Core;
use tokio_core::net::TcpStream;
fn main() {
let mut core = Core::new().expect("Could not create event loop");
let handle = core.handle();
let addr: SocketAddr = "127.0.0.1:9999".parse().expect("Could not parse as SocketAddr");
let socket = TcpStream::connect(&addr, &handle);
let request = socket.and_then(|socket| {
tokio_io::io::write_all(socket, &[65, 12])
});
let buf = vec![];
let response = request.and_then(|(socket, _request)| {
let sock = BufReader::new(socket);
tokio_io::io::read_until(sock, b'\n', buf)
});
let (_socket, data) = core.run(response).unwrap();
println!("{}", String::from_utf8_lossy(&data));
}
I am expecting this to print "A\n", since the ASCII code for A is 65 and newline is 12. My server is netcat using this command
ncat -l 9999 --keep-open --exec "/bin/cat"
This seems to hang on running the response future on the core. What am I missing here?