title: how to reproduce "Connection reset by peer"
body: <p>I have learned the differences between the two infamous errors in tcp:</p>
  
  <ul>
  <li><code>[Errno 54] Connection reset by peer</code></li>
  <li><code>[Errno 32] Broken pipe</code></li>
  </ul>
  
  <p>Both errors are one side of the tcp connection closed for unknown reason, and the other side still communicate with it. </p>
  
  <ul>
  <li>when the other side <code>write</code> something, <code>Broken pipe</code> is thrown</li>
  <li>when the other side <code>read</code> something, <code>Connection reset by peer</code> is thrown</li>
  </ul>
  
  <p>I was able to reproduce <code>Broken pipe</code> using Python codes below.</p>
  
  <pre><code># tcp_server.py
  def handler(client_sock, addr):
      try:
          print('new client from %s:%s' % addr)
      finally:
          client_sock.close()   # close current connection directly
  
  if __name__ == '__main__':
  
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.bind(('', 5500))
      sock.listen(5)
  
      while 1:
          client_sock, addr = sock.accept()
          handler(client_sock, addr)
  </code></pre>
  
  <p>As for client, </p>
  
  <pre><code>&gt;&gt;&gt; sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  &gt;&gt;&gt; sock.connect(('', 5500))
  &gt;&gt;&gt; sock.send('a')
  1
  &gt;&gt;&gt; sock.send('a')
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  socket.error: [Errno 32] Broken pipe
  </code></pre>
  
  <p>When the client first <code>send</code>, a <code>RST</code> packet is sent from server to client, from this moment on, <code>send</code> will always throw <code>Broken pipe</code>.</p>
  
  <p>Everything above is within my understanding. However when client read from server , it always return empty string instead of throw <code>Connection reset by peer</code></p>
  
  <pre><code>&gt;&gt;&gt; sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  &gt;&gt;&gt; sock.connect(('', 5500))
  &gt;&gt;&gt; sock.recv(1024)
  ''
  &gt;&gt;&gt; sock.recv(1024)
  ''
  &gt;&gt;&gt; sock.recv(1024)
  ''
  &gt;&gt;&gt; sock.recv(1024)
  </code></pre>
  
  <p>I am confused at this, or generally how to reproduce the <code>Connection reset by peer</code> ?</p>