Python Socket errno 10054 only when client.py runs
Question: There are some other posts about this issue but none did help me with mine.
I'm trying to build a total simple server - client relationship in python

server.py

    
    
    #!/usr/bin/python
    import socket
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(("127.0.0.1",8889))
    s.listen(1)   
    try:
    
        while True:
            client, add = s.accept()
            data = client.recv(1024)
            if not data:
                print 'No data'
            print data
    
    finally:
        s.close()
    

client.py

    
    
    #!/usr/bin/python
    import socket
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("127.0.0.1",8889))
    
    
    try:
        while True:
            message = 'Foo'
            s.send(message)
            ans = s.recv(1024)
            print ans
    finally:
        s.close()
    

I start by running the server first, but when I try to run the client I'm
getting this `Errno 10054 --> An existing connection was forcibly closed by
the remote host` While request the browser with the ip and the related port, I
receive some data. I'm quiet new to networking, so please explain what might
be obvious wrong in my code.

EDIT* Main issue is, that the client is somehow wrong, because it returns also
an empty string on recv

Thank you in advance


Answer: I am guessing:

The server accepts one socket and then does

    
    
        client, add = s.accept()
        data = client.recv(1024)
        ...
        client, add = s.accept()
    

The client does this in the mean time:

    
    
    s.send(message)
    ans = s.recv(1024) # blocks until timeout
    

If now an other client connects to the server then `client` is replaced, the
socket garbage collected and closed. `s.recv(1024)` will then tell that the
connection is reset.

Have a look at `import select` or twisted (google around) to handle multiple
connections at once.

