title: What exactly does the -q option of netcat do?
body: <p>I have a service whose behavior I've been debugging behind a proxy. The proxy is a black box, but the service's behavior can be simulated with a trivial python program like below:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import socket
  import sys
  import time
  
  PORT = int(sys.argv[1] or 50007)
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  try:
      s.bind(('', PORT))
      s.listen(1)
      conn, addr = s.accept()
      print('conn from %s' % (addr,))
      data = conn.recv(1024)
      time.sleep(1)
      conn.sendall(data)
  finally:
      conn.close()
      s.close()
  </code></pre>
  
  <p>When I connect to this directly with netcat, I get the expected behavior, which is that after sending some arbitrary text to stdin with a newline, the server delays for a second, echoes it back, and closes the connection. However, when I put this service behind the proxy, netcat instead immediately exits with status 0, <em>unless</em> I give it the -q option with some nonzero number, after which it behaves the same as the direct connection.</p>
  
  <p>To quote from the manpage for netcat on my system:</p>
  
  <blockquote>
  <pre><code>-q      after EOF on stdin, wait the specified number of seconds and then quit.
          If seconds is negative, wait forever.
  </code></pre>
  </blockquote>
  
  <p>What I'm trying to debug is what is <em>different</em> about the proxy connection behavior that would cause the connection behavior to differ with the -q option, and unfortunately, the man page isn't doing much to help me out. What is the "stdin" it's referring to, if it's not my input (which never receives an EOF from me)? What EOF is -q ignoring for long enough to receive the proxy'd data back from that it doesn't have to ignore when there is no proxy mediating the connection?</p>
  
  <p>EDIT: On request, here are some sample invocations, although they're about as basic as it gets:</p>
  
  <pre><code># after starting the example server on &lt;someserver&gt; with &lt;someport
  
  $ echo foo | nc &lt;someserver&gt; &lt;someport&gt;
  foo
  $
  
  # the above, just with -q1 (no change)
  $ echo foo | nc -q1 &lt;someserver&gt; &lt;someport&gt;
  foo
  $
  
  # after starting the example server behind the blackbox proxy
  $ echo foo | nc &lt;someproxy&gt; &lt;someproxyport&gt;
  $
  
  # same, but with -q
  $ echo foo | nc -q1 &lt;someproxy&gt; &lt;someproxyport&gt;
  foo
  $
  </code></pre>