title: Python Client-server problems
body: <p>I am making a chat program.
  I have a (TCP) server which creates a new thread for every connection request it gets.   </p>
  
  <ol>
  <li>I'm having problems when the client quits/terminates connection. The server raises an error(below). How do I handle it?  </li>
  <li>And, the server has to send the 'data' it receives from one client to another (changeable) client.<br>
  How do I implement this??</li>
  </ol>
  
  <p>I receive this error when the client quits :</p>
  
  
  
  <pre class="lang-py prettyprint-override"><code>Exception in thread Thread-1:
      Traceback (most recent call last):
        File "C:\Python2.7 For Chintoo\lib\threading.py", line 552, in __bootstrap_inner
          self.run()
        File "C:\Python2.7 For Chintoo\lib\threading.py", line 505, in run
          self.__target(*self.__args, **self.__kwargs)
        File "C:\Users\karuna\Desktop\Jython\Python\My Modules\Network\Multi-server.py", line 23, in recv_loop
          data = client.recv(1024)
      error: [Errno 10054] An existing connection was forcibly closed by the remote host
  </code></pre>
  
  <p>My scripts:</p>
  
  <p><strong>Multi-server.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>import os, socket, time, threading, random
  
  class Server:
      def __init__(self,host,port,user):
          self.port = port
          self.host = host
          self.user = user
          self.bufsize = 1024
          self.addr = (host,port)
  
          self.socket = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
          self.socket.bind(self.addr)
          print "Server running on",host,"at port",port
  
          self.socket.listen(5)
  
  def recv_loop(server,client,caddr):
      print 'Connected To',caddr
  
      while True:
          global clients
          name = clients[client]
          data = client.recv(1024)
          if not data:
              break
          print name + " said: " + data
      client.close()
  
  
  host = 'localhost'
  port = random.randint(1025,60000)
  user = 'No one'
  
  server = Server(host, port, user)
  
  clients = {}
  threads = []
  while True:
      client, caddr = server.socket.accept()
      # name extraction
      name = client.recv(1024)
  
      clients[client] = name
  
      thread = threading.Thread(target=recv_loop, args=(server,client, caddr))
      thread.start()
  </code></pre>
  
  <p><strong>client.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>from socket import *
  
  host = 'localhost'
  name = raw_input('Enter name: ')
  port = int(raw_input('Enter server port: '))
  bufsiz = 1024
  addr = (host, port)
  
  tcpClient = socket(AF_INET , SOCK_STREAM)
  tcpClient.connect(addr)
  
  # sending name
  tcpClient.send(name)
  
  while True:
      data = raw_input('&gt; ')
      if not data:
          break
      tcpClient.send(data)
  raw_input('Enter to Quit')
  </code></pre>