title: How to properly close socket in both client and server (python)
body: <p>I am writing 2 scripts in python.</p>
  
  <ol>
  <li>Client.py</li>
  <li>Server.py</li>
  </ol>
  
  <p>There is a socket between the client and server.
  The scenario is this:<br>
  I am having one client that ask to close the program therefore it should inform the server which by then will inform the other client, therefore I need to close the socket from client (1) to the server and then close the socket from the server to other client (imagin yourself a game of 2 people that one ask to exit the game).</p>
  
  <p>I am doing it like that.In Client.py:</p>
  
  <pre><code># send the request to the server
  eNum, eMsg = Protocol.send_all(self.socket_to_server, msg)
  if eNum:
      sys.stderr.write(eMsg)
       self.close_client()
  self.socket_to_server.shutdown(socket.SHUT_WR) 
  exit(0)
  </code></pre>
  
  <p>then in the code of Server.py:</p>
  
  <pre><code># get the msg from the client that calleds
          num, msg = Protocol.recv_all(self.players_sockets[0])
  
          # case of failure
          if num == Protocol.NetworkErrorCodes.FAILURE:
              sys.stderr.write(msg)
              self.shut_down_server()
  
          # case it was disconnected
          if num == Protocol.NetworkErrorCodes.DISCONNECTED:
              print msg
              self.shut_down_server()
  
  
  
          technical_win = ("exit" == msg)
  
  
  
          # send the msg to the client needed to call    
          eNum, eMsg = Protocol.send_all(self.players_sockets[1],msg)
          if eNum:
              sys.stderr.write(eMsg)
              self.shut_down_server()
  
          # case end of game (winning or asking to exit)
          if technical_win:
              self.shut_down_server()
  </code></pre>
  
  <p>while <code>self.shut_down_server()</code>is:</p>
  
  <pre><code>def shut_down_server(self):
  
          # close socket of one of the player
          self.players_sockets[0].shutdown(socket.SHUT_RDWR)
          self.players_sockets[0].close()
  
          # close socket of one of the player
          self.players_sockets[1].shutdown(socket.SHUT_RDWR)
          self.players_sockets[1].close()     
  
          # clean up
          exit(0)  
  </code></pre>
  
  <p>When the server send the msg that the other player ask to exit the client get it and doing the same as I showed in the begining.</p>
  
  <p>unfortunately it is not working because when the server perform the code:</p>
  
  <pre><code>num, msg = Protocol.recv_all(self.players_sockets[0])
  
          # case of failure
          if num == Protocol.NetworkErrorCodes.FAILURE:
              sys.stderr.write(msg)
              self.shut_down_server()
  
          # case it was disconnected
          if num == Protocol.NetworkErrorCodes.DISCONNECTED:
              print msg
              self.shut_down_server()
  </code></pre>
  
  <p>it get into the second if and print <code>'ERROR - Could not receive a message: timed out.'</code></p>
  
  <p>How do I fix this properly?</p>
  
  <p>P.S</p>
  
  <p>I am using Linux</p>