Python socket.error: [Errno 104] Connection reset by peer

前端 未结 3 515
终归单人心
终归单人心 2021-02-06 03:07

I have a code where there are 13 clients that have to connect to the server. Then the server does some counting on the data given by the client. After that the roles turns aroun

相关标签:
3条回答
  • 2021-02-06 03:19

    I had this exact error when trying to connect to a remote redis instance from python, because I had mistakenly left Fiddler running and it was interfering with the requests.

    0 讨论(0)
  • 2021-02-06 03:22

    You are going to have to use threading on the server.
    For a simple explanation see: http://www.binarytides.com/python-socket-server-code-example/
    The guts of the simple example given there is:

    import socket
    import sys
    from thread import *
    
    HOST = ''   # Symbolic name meaning all available interfaces
    PORT = 8888 # Arbitrary non-privileged port
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print 'Socket created'
    
    #Bind socket to local host and port
    try:
        s.bind((HOST, PORT))
    except socket.error as msg:
        print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
        sys.exit()
    
    print 'Socket bind complete'
    
    #Start listening on socket
    s.listen(10)
    print 'Socket now listening'
    
    #Function for handling connections. This will be used to create threads
    def clientthread(conn):
        #Sending message to connected client
        conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
    
        #infinite loop so that function do not terminate and thread do not end.
        while True:
    
            #Receiving from client
            data = conn.recv(1024)
            reply = 'OK...' + data
            if not data: 
                break
    
            conn.sendall(reply)
    
        #came out of loop
        conn.close()
    
    #now keep talking with the client
    while 1:
        #wait to accept a connection - blocking call
        conn, addr = s.accept()
        print 'Connected with ' + addr[0] + ':' + str(addr[1])
    
        #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
        start_new_thread(clientthread ,(conn,))
    
    s.close()
    
    0 讨论(0)
  • 2021-02-06 03:40

    It seems that the clients were connected to the server but they encountered with " [Errno 104] Connection reset by peer" exception when they tried to send data. For the first time, Python raises "[Errno 104] Connection reset by peer" exception, then for the second time and more you would get "[Errno 32] Broken pipe" exception on the client side.

    This can mean that the server is up and listening on the port (otherwise, you would get "[Errno 111] Connection refused" exception on the client side". This also means that the server is crashed before closing the connection since if the connection was closed on the server side before sending data on the client side, the client would encounter with "[Errno 32] Broken pipe" exception.

    "Connection reset by peer" is the TCP/IP equivalent of slamming the phone back on the hook. It's more polite than merely not replying, leaving one hanging. But it's not the FIN-ACK expected of the truly polite TCP/IP converseur. (From another stackoverflow answer)

    0 讨论(0)
提交回复
热议问题