尝试编写命名管道时如何修复 "Broken Pipe" 错误?
How to fix "Broken Pipe" error when trying to write a named pipe?
我正在试验 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
why it happens?
broken pipe
表示:您已尝试写入一个管道(当前)
没有 reader.
在接收器中,您不断地关闭和重新打开 FIFO。如果发送方试图在接收方 close
和 open
之间写入,它将从 write(2)
.
得到 EPIPE
错误
所以不要那样做 -- 打开 FIFO 一次 并继续从中读取消息。
我正在试验 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
why it happens?
broken pipe
表示:您已尝试写入一个管道(当前)
没有 reader.
在接收器中,您不断地关闭和重新打开 FIFO。如果发送方试图在接收方 close
和 open
之间写入,它将从 write(2)
.
EPIPE
错误
所以不要那样做 -- 打开 FIFO 一次 并继续从中读取消息。