我正在尝试按照 Tokio 客户端教程编写一个与回显服务器对话的客户端,该回显服务器在最后用换行符发回响应。这是我所拥有的:
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));
}
我希望这会打印“A\n”,因为 A 的 ASCII 码是 65,换行符是 12。我的服务器是 netcat 使用这个命令
ncat -l 9999 --keep-open --exec "/bin/cat"
这似乎取决于在核心上运行响应未来。我在这里想念什么?