Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

sockets

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 around - the server becomes a client and clients become servers to receive the data. The thing is that when trying to do the first connection, that is when the 13 clients try to connect to the server I keep getting this error: [Errno 104] Connection reset by peer. I tried some workarounds for example trying to connect 5 times in a second interval but nothing works.

Here my code:

server.py

import socket, pickle, numpy as np
import struct
import math


while 1:
    HOST = ''
    PORT = 50007
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(13)

adresses = []
ports = []

i = 0
print("receiving...")
while i < 13:
    i += 1    
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print ('Connected with ', addr)
    adresses.append(addr[0])
    buf = b''
    while len(buf) < 4:
        buf += conn.recv(4 - len(buf))
    length = struct.unpack('>I', buf)[0]
    data = b''
    l = length

    while l > 0:

        d = conn.recv(l)
        l -= len(d)
        data += d

    if not data: break

    M = np.loads(data)

    if i == 1:
        L = M[0]
    else:
        L += M[0]
    ports.append(M[1])
    conn.close() 
s.close()


L /= 993040

packet = pickle.dumps(L)
length = struct.pack('>I', len(packet))
packet = length + packet

print("sending...")
for kl, addr in enumerate(adresses):
    HOST = addr
    PORT = 50007 + ports[kl]
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.sendall(packet)
    s.close()

client.py

def connection(centers, kl):

    HOST = "192.168.143.XX"
    PORT = 50007
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(3600)
    try:
        s.connect((HOST, PORT)) # HERE IS AN ERROR
        s.settimeout(None)
        packet = pickle.dumps([centers, kl]) ## ???
        length = struct.pack('>I', len(packet))
        packet = length + packet
        s.sendall(packet) # OR HERE IS AN ERROR
        s.close()
    except Exception as e:
        print(e)
        print('error ', kl)
        s.close()
        return np.zeros(centers.shape)    


    HOST = ''
    PORT = 50007 + kl
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))

    s.listen(2)

    i = 0
    while i < 1:
        #wait to accept a connection - blocking call
        conn, addr = s.accept()
        i += 1
        print ('Connected with ', addr)
        buf = b''
        while len(buf) < 4:
            buf += conn.recv(4 - len(buf))

        length = struct.unpack('>I', buf)[0]
        data = b''
        l = length
        while l > 0:
            d = conn.recv(l)
            l -= len(d)
            data += d
        if not data: break

        new_centers = np.loads(data)
        conn.close()

    s.close()
    return new_centers

aa = 0
for k in range(99):
    print(k)
    centers = some_function(centers)
    time.sleep(60)
    centers1 = connection(centers, i)
    aa = 0
    while not (centers1.any()) and aa < 5:
        time.sleep(1)
        centers1 = connection(centers, i)
        aa += 1
    centers = centers1

The thing is all of the 13 clients HAVE TO connect to the server or it won't proceed to the next iteration. I'm using Python 3.4. Please help.

Update:

I have added threads but the error remains:

[Errno 104] Connection reset by peer

server.py

import socket, pickle, numpy as np
import struct
import math
from multiprocessing.pool import ThreadPool

def clientthread(conn, L):
    buf = b''
    while len(buf) < 4:
        buf += conn.recv(4 - len(buf))
    length = struct.unpack('>I', buf)[0]
    data = b''
    l = length
    while l > 0:
        d = conn.recv(l)
        l -= len(d)
        data += d
    M = np.loads(data)


    return(M)


j = 0
while 1:
    HOST = ''
    PORT = 50007
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(2)
    #print('0')

    adresses = []
    ports = []

    i = 0
    print("receiving...")
    while i < 13:
        i += 1    
        #wait to accept a connection - blocking call
        conn, addr = s.accept()
        print ('Connected with ', addr)
        adresses.append(addr[0])
        pool = ThreadPool(processes=13)
        async_result = pool.apply_async(clientthread, (conn, i,))
        M = async_result.get()  
        conn.close() 

        if i == 1:
            L = M[0]
        else:
            L += M[0]
        ports.append(M[1])
    s.close()


    L /= 993040

    packet = pickle.dumps(L)
    length = struct.pack('>I', len(packet))
    packet = length + packet

    for kl, addr in enumerate(adresses):
        HOST = addr
        PORT = 50007 + ports[kl]
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((HOST, PORT))
        s.sendall(packet)
        s.close()
like image 551
potockan Avatar asked Nov 29 '15 09:11

potockan


1 Answers

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)

like image 111
Mahyar Hosseini Avatar answered Nov 15 '22 16:11

Mahyar Hosseini