Bidirectional socat functionality with nc

netcatsocat

This is question is somehow related to
https://superuser.com/questions/270698/how-is-it-called-when-listeningconnecting-two-sockets-and-exchanging-data-betw

In the test setup I have three terminal windows open
I run in

Term1: "nc -l 55545"
Term2: "nc -l 55546"
Term3: "socat tcp:localhost:55545 tcp:localhost:55546"

Input to Term1 now appears in Term2 and
input to Term2 appears in Term1.

This is the desired behavior.
How can I achieve this behavior by only
using nc?

When I run in

Term3: "nc localhost 55545 | nc localhost 55546"

then Input to Term1 appears in Term2 but
input to Term2 appears in Term3.
How can I make the pipe bidirectional?
If possible without temporary files.

Best Answer

This comes straight from the Wikipedia page on netcat. In Term3, you would run:

mkfifo backpipe
nc localhost 55545 0<backpipe | nc localhost 55546 1>backpipe

This does pretty much exactly what you want. It uses a FIFO to get output from the left-hand side back into the right-hand side. It's not, strictly speaking, a temporary file -- a FIFO is a named pipe between two processes.

Related Question