- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个问题的这个小示例版本:
#[test]
fn streams() {
use std::io::prelude::*;
use std::net::Shutdown;
use std::os::unix::net::UnixStream;
use std::time::Duration;
let (mut s1, mut s2) = UnixStream::pair().unwrap();
s1.write_all(b"hello world").unwrap();
// Problem is that if not calling shutdown message never arrives
s1.shutdown(Shutdown::Write).unwrap();
let mut response = String::new();
s2.read_to_string(&mut response).unwrap();
assert_eq!("hello world".to_string(), response);
}
我需要关闭连接,否则消息永远不会到达。我认为问题在于 write_all
不写入 EOF,因此在使用 read_to_string
时挂起。
例如在 python 中我会简单地使用:
socket.sendall(message.encode())
data = socket.recv(1024)
发送和接收回复。
我怎样才能用 Rust 实现同样的效果?
提前谢谢你。
最佳答案
read_to_string
的语义是:
Read all bytes until EOF in this source, appending them to buf.
If successful, this function returns the number of bytes which were read and appended to buf.
所以如果你不关闭流它就会挂起。您想改用 read
:
let (mut s1, mut s2) = UnixStream::pair().unwrap();
s1.write_all(b"hello world").unwrap();
let mut buf = [0; 1024];
let count = s2.read(&mut buf).unwrap();
let response = String::from_utf8(buf[..count].to_vec()).unwrap();
assert_eq!("hello world".to_string(), response);
关于rust - UnixStream 写入/读取完整字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57646423/
我有一个问题的这个小示例版本: #[test] fn streams() { use std::io::prelude::*; use std::net::Shutdown;
我正在尝试从 UnixStream(在此代码中称为 socket)读取一些未知大小的数据。数据由 6 个字节的 header 组成,最后两个字节表示消息的其余部分应该有多长。 .into_buf()我
tokio::run_async + futures 0.3 + tokio::net::UnixStream panic 。 设置 [package] name = "prac" version =
我是一名优秀的程序员,十分优秀!