title: python3 - broken pipe error when using socket.send()
body: <p>I am programming a client-server instant message program. I created a similar program in Python 2, and am trying to program it in Python 3. The problem is when the server takes the message and tries to send it to the other client, it gives me "[Errno 32] Broken Pipe" and exits.</p>
  
  <p>I have done some research, and found that this occurs when the client disconnects, so I did some more testing but could not find when the client disconnects. (I am using Ubuntu 14.04 and Python 3.4)</p>
  
  <p>Here is the server code:</p>
  
  <pre><code>import socket, select, sys
  
  def broadcast(sock, messaged):
      for socket in connection_list:
          if socket != s and socket != sock:
          # Here is where it gives me the broken pipe error
              try:
                  s.send(messaged.encode("utf-8"))
              except BrokenPipeError as e:
                  print(e)
                  sys.exit()
  connection_list = []
  host = ''
  port = 5558
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  s.bind((host,port))
  s.listen(5)
  
  connection_list.append(s)
  read_sockets,write_sockets,error_sockets = select.select(connection_list,[],[])
  while True:
      for sock in read_sockets:
          if sock == s:
              conn, addr = s.accept()
              connection_list.append(conn)
              client = "Client (%s,%s) connected" % addr
              print(client)
              broadcast(sock,client)
  
          else:
              try:
                  data = sock.recv(2048)
                  decodeddata = data.decode("utf-8")
                  if data:
                      broadcast(sock, decodeddata)
              except:
                  offline = "Client " + addr + "is offline"
                  broadcast(sock, offline)
                  print(offline)
                  connection_list.remove(sock)
                  sock.close()
                  continue
  </code></pre>
  
  <p>And the client code:</p>
  
  <pre><code>import socket, select, string, sys, time
  
  def prompt(data) :
       print("&lt;You&gt; " + data)
  def Person(data) :
      print("&lt;Receiver&gt; " + data)
  
  if __name__ == "__main__":
  
      host = "localhost"
      port = 5558
  
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.settimeout(2)
  
      try:
          s.connect((host,port))
      except:
          print('Unable to connect')
          sys.exit()
      print('Connected.')
      socket_list = [s]
      read_sockets,write_sockets,error_sockets = select.select(socket_list,[],[])
      while 1:
          for sock in read_sockets:
              if sock == s:
                  try:
                      time.sleep(1)
                      data = sock.recv(1024)             
                      Person(data.decode("utf-8"))
                  except:
                      msg = input("Send a message: ")
                      try:
                          s.send(str.encode(msg))
                      except:
                          print("Server is offline")
                          sys.exit()
  
  
              else:
                  print("Server is offline")
                  sys.exit()
  </code></pre>