Hi, I'm trying to learn more Rust by writing the examples in Beej's Guide to Network Programming from C to Rust. ![]()
I have this block code (TCP server accepting connections from a client):
match listener.accept() {
Ok((mut stream, addr)) => {
println!("connection accepted: {:?}", addr);
match fork() {
Ok(ForkResult::Child) => {
stream.write(b"Hello world!")?;
stream.flush()?;
stream.shutdown(Shutdown::Both).expect("Child shutdown failed")
},
Ok(ForkResult::Parent {child: _, ..}) => {
println!("Parent...");
stream.shutdown(Shutdown::Both).expect("Parent shutdown failed")
},
Err(_) => {
println!("fork() failed")
},
}
},
Err(e) => println!("attempt from client: {:?} failed", e),
}
I am using the nix crate to fork() a new process. I was expecting the stream in both the parent block and the child block to be different copies. But, it looks like they are the same. Running the code above, I get the following error when a client connects:
Parent...
Error: Os { code: 32, kind: BrokenPipe, message: "Broken pipe" }
But if I comment out the stream.shutdown() call in the parent block, then the client can connect and receive the message.
Is this behavior inherent to the Rust language? -OR- is it an implementation specific behavior and I should be asking the folks from nix for clarifications about this?