close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python socket.: [Errno 104] Connection reset by peer in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python socket.: [Errno 104] Connection reset by peer Error Occurs?

Today I get the following error Python socket.: [Errno 104] Connection reset by peer in python.

How To Solve Python socket.: [Errno 104] Connection reset by peer Error ?

  1. How To Solve Python socket.: [Errno 104] Connection reset by peer Error ?

    To Solve Python socket.: [Errno 104] Connection reset by peer Error 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.

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

    To Solve Python socket.: [Errno 104] Connection reset by peer Error 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.

Solution 1


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. 

Solution 2

You are going to have to use threading on the server.
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()

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read