title: How to wire 2 remote processes together via netcat and fifo?
body: <p>On HostA, I have a process, <code>procA</code>, that does I/O, like so:</p>
  
  <pre><code>#!/bin/bash
  while sleep 1 ; do
    # Consume input *if available*
    if read -t 0.5 x; then  # a timed-read
      # If input is available, output it.
      echo "HostA Input: $x"
    fi
  
    # Produce some output.
    echo "HostA Output: $(date)"
  done
  </code></pre>
  
  <p>On HostB, I have a very similar process, <code>procB</code>, with the only difference that <code>procB</code> tags its input/output with the prefix <code>HostB</code> (instead of the prefix <code>HostA</code>).</p>
  
  <p>Now, I want to use the netcat program (<code>/usr/bin/nc</code>) to send <code>procA</code>'s stdout to <code>procB</code>'s stdin, and <code>procB</code>'s stdout to <code>procA</code>'s stdin. My expectation in all this is that HostA's output should appear on HostB, and HostB's output should appear on HostA... much as would happen in following <strong>canonical <code>nc</code> example</strong> of a peer-to-peer chat:</p>
  
  <pre><code>[HostB]$ nc -l 4000
  
  [HostA]$ nc HostB 4000
  </code></pre>
  
  <p>But, when I tried to extend the above canonical example by replacing the human at both hosts with processes <code>procA</code> and <code>procB</code> as follows, <strong>it did not work</strong>:</p>
  
  <p>On <strong>HostB</strong>:</p>
  
  <pre><code>$ mkfifo p
  $ ./procB &lt;p | nc -l 4000 &gt;p
  &lt;WHY NO OUTPUT HERE??&gt;
  </code></pre>
  
  <p>and, on <strong>HostA</strong>:</p>
  
  <pre><code>$ mkfifo p
  $ ./procA &lt;p | nc HostB 4000 &gt;p
  &lt;WHY NO OUTPUT HERE??&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Notes:</strong></p>
  
  <ol>
  <li>I have verified that port 4000 is open on HostB.</li>
  <li><strong>Unidirectional</strong> communication, such as the following, <strong>seems to work</strong>:</li>
  </ol>
  
  <p>On <strong>HostB</strong>:</p>
  
  <pre><code>$ mkfifo p
  $ ./procB &lt;p | nc -l 4000 &gt;p
  </code></pre>
  
  <p>On <strong>HostA</strong>:</p>
  
  <pre><code>$ ./procA | nc HostB 4000 
  HostB Output: Wed Jan 11 13:53:37 IST 2012
  HostB Output: Wed Jan 11 13:53:39 IST 2012
  HostB Input: HostA Output: Wed Jan 11 13:55:02 IST 2012
  HostB Output: Wed Jan 11 13:53:40 IST 2012
  HostB Input: HostA Output: Wed Jan 11 13:55:04 IST 2012
  HostB Output: Wed Jan 11 13:53:41 IST 2012
  HostB Input: HostA Output: Wed Jan 11 13:55:05 IST 2012
  HostB Output: Wed Jan 11 13:53:42 IST 2012
  ^C
  </code></pre>
  
  <p>Now, I know that <code>nc</code> comes with an INSECURE option <code>-e</code> that could somehow be employed if it's enabled in your <code>nc</code> binary, but assuming that I don't have <code>-e</code> option available, how can I connect 2 remote processes to each other via <code>nc</code>?  Basically, where I'm going wrong above? Why there is no output in the places I have indicated above? Many thanks in advance.</p>