I've been fiddling with getting this to work for a while now, so I suspect some sort of fundamental misunderstanding about how pipes work is the root cause of my troubles.
My goal is to initiate a TCP connection to some remote host via netcat
and have two named pipes on the filesystem: one that processes can read from to get incoming data, and other that processes can write to that serve as outgoing data. I am presently using the following construction:
mkfifo in
mkfifo out
cat out | netcat foo.bar.org 4000 > in &
From here, I would like to permit other processes to read and write to/from this open TCP connection. Should this "just work", or is there a reason why a construct like this can't work?
What seems to happen at present is that I can read from out
without issue, but when I write to in
I get output mentioning a broken pipe and all subsequent communication appears to be dead. Thoughts?
(Related: I originally used:
netcat foo.bar.org 4000 < out > in &
but found it to block waiting for input. I'm curious about this as well, but it's probably better addressed in a separate question.)
Best Answer
I think the problem is that
cat
will exit as soon as it receives anEOF
from theout
pipe. And whencat
exits, the rest of the pipeline (includingnetcat
) gets terminated as well.Try something like this instead:
Thus,
cat
gets restarted as often as needed, and anyEOF
s appearing in theout
pipe are effectively handled.