【发布时间】:2021-12-26 00:56:06
【问题描述】:
我正在尝试使用 rust 中的命名管道,并且我想创建一个服务器来接收来自永不结束的客户端的消息。
//reciever.rs
use libc::{c_char, mkfifo};
use std::ffi::CString;
use std::fs::OpenOptions;
use std::io::Read;
fn main() {
let _ = std::fs::remove_file("rust-fifo");
let name_fifo = CString::new("rust-fifo").unwrap();
let name_fifo: *const c_char = name_fifo.as_ptr() as *const c_char;
if unsafe { mkfifo(name_fifo, 0o644) } != 0 {
panic!("Error creating fifo.")
}
loop {
let mut file = OpenOptions::new().read(true).open("rust-fifo").unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();
//println!("{:#?}", &buffer);
println!("{}", String::from_utf8(buffer).unwrap());
}
}
//sender.rs
use std::{fs::OpenOptions, io::Write};
fn main() {
loop {
let mut file = OpenOptions::new().write(true).open("rust-fifo").expect("error opening the file");
file.write_all(b"hello").expect("error writing the file");//ERROR HERE "BROKEN PIPE"
}
}
receiver.rs 收到一些消息但随后sender.rs 抛出错误,当不使用循环时一切正常,但我希望有一个永远不会结束的客户端,这是错误,为什么会发生?
thread 'main' panicked at 'error writing the file: Os { code: 32, kind: BrokenPipe, message: "Broken pipe" }', src/bin/sender.rs:6:34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
【问题讨论】:
标签: rust pipe named-pipes