我有以下来自 Tokio 文档的工作代码,我稍作修改:
// Task
let connection = io::read_exact(socket, buf_read)
.and_then(|(socket, buf_read)| {
println!("Do something with the received data...");
for b in &buf_read {
println!("{}", b);
}
// Write to the socket
let buf_write = vec![19; 30];
io::write_all(socket, buf_write)
})
.then(|res| {
println!("{:?}", res); // Just for testing
//Output: Ok((TcpStream, [19, 19, 19, ...]
println!("Send data...");
let buf_write = vec![18; 10]; // Fill the buffer with some data
//
//How to use the socket contained in res to write the data to the socket
//
Ok(())
});
请注意,这
res
是一个Result
包含原始套接字的。这允许我们在同一个套接字上对额外的读取或写入进行排序。
如何使用包含在套接字中的套接字Result
向套接字写入数据?