Python Client-server problems
Question: I am making a chat program. I have a (TCP) server which creates a new thread
for every connection request it gets.

  1. I'm having problems when the client quits/terminates connection. The server raises an error(below). How do I handle it? 
  2. And, the server has to send the 'data' it receives from one client to another (changeable) client.  
How do I implement this??

I receive this error when the client quits :

    
    
    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
    

My scripts:

**Multi-server.py**

    
    
    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()
    

**client.py**

    
    
    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('> ')
        if not data:
            break
        tcpClient.send(data)
    raw_input('Enter to Quit')
    


Answer: I haven't done socket programming in Python, but you might want to cleanly
close your socket connection before the client quits. I would use `close`
method in the client.

