title: Connection reset by peer [errno 104] in Python 2.7
body: <p>I've seen and read a lot about this particular issue on the internet.
  I am writing a simple chat server and client using socket in python for learning purpose mainly.</p>
  
  <p>I've observed an issue here.</p>
  
  <p>Here is my server code :</p>
  
  <pre><code>__author__ = 'pchakraverti'
  
  import socket
  import select
  import sys
  
  
  class NickSocketMap(object):
      count = 0
  
      def __init__(self, nick, client_socket):
          self.nick = nick
          self.client_socket = client_socket
          NickSocketMap.count += 1
  
      @staticmethod
      def display_count():
          print "Total number of clients is %d" % NickSocketMap.count
  
  
  host = ""
  port = 7575
  socket_list = []
  nick_list = []
  cnt = 0
  recv_buffer = 1024
  
  
  def register_nick(nick, client_socket):
      obj = NickSocketMap(nick, client_socket)
      nick_list.append(obj)
  
  
  def process_request(request_string, client_socket):
      parts = request_string.split("|")
      if parts[0] == "set_nick":
          register_nick(parts[1], client_socket)
          client_socket.send("nick_set")
      elif parts[0] == "transmit_msg":
          broadcast_message(parts[1], parts[2])
      return 1
  
  
  def broadcast_message(message, client_nick):
      for s in nick_list:
          if s.nick == client_nick:
              try:
                  s.client_socket.send(message)
              except socket.errno, ex:
                  print ex
              break
  
  
  def run_server():
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      try:
          sock.bind((host, port))
      except socket.errno, ex:
          print ex
          sys.exit(-1)
      sock.listen(10)
      # add the parent socket in the list
      socket_list.append(sock)
      # keep the server alive
      while True:
          try:
              read_ready, write_ready, in_error = select.select(socket_list, [], [], 0)
          except select.error, ex:
              print ex
              continue
          for s in read_ready:
              # check if s is the parent socket
              if s == sock:
                  # accept new connection and append to list
                  try:
                      con, addr = s.accept()
                      if con not in socket_list:
                          socket_list.append(con)
                  except socket.errno, ex:
                      print ex
              else:
                  try:
                      # receive packet from connected client
                      packet = s.recv(recv_buffer)
                      if not packet:
                          socket_list.remove(s)
                          read_ready.remove(s)
                          for n in nick_list:
                              if n.client_socket == s:
                                  nick_list.remove(n)
                                  break
                          break
                      print packet
                  except socket.errno, ex:
                      print ex
                      continue
                  process_request(packet, s)
      sock.close()
  
  
  if __name__ == "__main__":
      run_server()
  </code></pre>
  
  <p>and here is my client code:</p>
  
  <pre><code>__author__ = 'pchakraverti'
  
  import socket
  
  
  nick = ""
  host = "192.168.0.167"
  port = 7575
  sock = ""
  
  
  def welcome():
      print "Welecome to SecuChat!"
      print "---------------------"
  
  
  def init():
      nick = raw_input("Enter your chat nickname : ")
      print nick
      global sock
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      try:
          sock.connect((host, port))
      except socket.errno, ex:
          print ex
      sock.send("set_nick|"+nick)
      #sock.close()
  
  if __name__ == "__main__":
      welcome()
      init()
  </code></pre>
  
  <p>In the client code, when I don't do the <code>sock.close()</code>, the server runs into an exception : </p>
  
  <pre><code>Traceback (most recent call last):
    File "server.py", line 102, in &lt;module&gt;
      run_server()
    File "server.py", line 84, in run_server
      packet = s.recv(recv_buffer)
  socket.error: [Errno 104] Connection reset by peer
  </code></pre>
  
  <p>how ever, when I add that line, the problem doesn't occur.</p>
  
  <p>Now I've two questions : </p>
  
  <p>i) I've handled exceptions in the server.py, why is this exception not being handled and why is it crashing the code ? How can I make the server more robust and what am I missing ?</p>
  
  <p>ii) What is the logic behind this crash and exception in relation to the <code>sock.close()</code> line in the client ?</p>