简体   繁体   English

如何处理TcpStream上的并行读写?

[英]How do I handle parallel reads and writes on a TcpStream?

I read Idiomatic way to handle writes to a TcpStream while waiting on read , but I'm still unsure of how to handle this. 在等待read时阅读了处理TcpStream的惯用方式 ,但是我仍然不确定如何处理。 I'm connecting to a Rust binary via Telnet and would like to send "commands" and receive "status". 我正在通过Telnet连接到Rust二进制文件,并希望发送“命令”并接收“状态”。 Almost like a simple echo server. 几乎就像一个简单的回显服务器。

I ended up cloning the stream and it's working fine: 我最终克隆了流 ,并且工作正常:

let second_stream = stream.try_clone().expect("Cannot clone stream");
let mut reader = BufferedReader::new(second_stream);
let mut writer = BufferedWriter::new(stream);

Just an update for Rust 1.1, this method no longer works. 只是Rust 1.1的更新,此方法不再起作用。 To make this work, I used stream.shutdown(Shutdown::Write) when I was finished with writing to the server socket. 为了完成这项工作,在完成对服务器套接字的写入后,我使用了stream.shutdown(Shutdown::Write) I don't know how I would do this if I had to repeatedly read and write from the same connection. 如果必须重复从同一连接读取和写入,我不知道该怎么办。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM