title: Is there a way to check if a socket is still running?
body: <p>I need to make a chat between two clients. But I don't know when a socket is closed or not, is there a way to check if it is?</p>
  
  <p>Here is the part of the code that I need to fix:</p>
  
  <pre><code>def main():
      """Implements the conversation with server."""
      # Open client socket, Transport layer: protocol TCP, Network layer: protocol IP
      client_socket = socket.socket()
      client_socket.connect((HOST_IP, PORT))
  
      # start conversation with new client in parallel thread
      name = input("enter your name ")
      protocol.send_request(client_socket, name)
      thread_for_responses = threading.Thread(target=get_responses,
                                              args=(client_socket, ))
      thread_for_responses.start()
  
      while True:
          # Get request from keyboard
          client_request_str = input()
          if client_request_str:  # if client_request_str not empty string
              # send request according to the protocol
              protocol.send_request(client_socket, client_request_str)
              # Get response from server
  </code></pre>
  
  <p>Instead of <code>while True</code>, I need to check if the socket is closed so it won't go into a loop where it will crush for using a closed socket.</p>