3

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.

11

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.

  • or do I have that wrong? seems like you need to reference the variable with $ sign and you can leave off the 0 and 1 file descriptors? – Alexander Mills Apr 21 '18 at 6:58
  • 1
    You can of course always use implicit file descriptors. I was just being explicit when I wrote this 7 years ago. I'm not sure what variable you're referring to; there are no variables in my answer. – larsks Apr 21 '18 at 18:48
  • nice that works, I follow – Alexander Mills Apr 21 '18 at 20:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.