我正在尝试使用 tokio 接收单个数据包:
extern crate tokio;
extern crate tokio_io;
use tokio::net::{TcpListener};
use tokio::prelude::*;
use std::net::SocketAddr;
fn main() {
let addr = "0.0.0.0:8080".parse::<SocketAddr>().unwrap();
let socket = TcpListener::bind(&addr).unwrap();
println!("Listening on: {}", addr);
let done = socket
.incoming()
.map_err(|e| println!("failed to accept socket; error = {:?}", e))
.for_each(move |mut socket| {
let mut bytes = vec![];
bytes.reserve(1024);
let processor = socket.read_buf(&mut bytes).into_future()
.and_then(move |_size| {
println!("bytes: {:?}", bytes);
Ok(())
})
.map_err(|_| ());;
tokio::spawn(processor)
});
tokio::run(done);
}
此代码打印一个空包。如何更改此代码以打印接收到的数据包?