8

I wrote a client-server python program where the client sends a list to the server, the server receives the array, deletes the first two elements of the list and sends it back to the client. There is no problem with the server receiving the list. But when the server wants to send back the edited list, it is showing error: socket.error: [Errno 32] Broken pipe. The client.py and the server.py are running from different machines with different ip. I'm posting the code for the client.py and server.py below:

Client.py

import socket, pickle
HOST = '192.168.30.218'
PORT = 50010
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
arr = ['CS','UserMgmt','AddUser','Arnab','Password']
data_string = pickle.dumps(arr)
s.send(data_string)
data = s.recv(4096)
data_arr1 = pickle.loads(data)
s.close()
print 'Received', repr(data_arr1)
print data_arr1;

Server.py:

import socket, pickle;
HOST = '127.0.0.1';
PORT = 50010;
s= socket.socket(socket.AF_INET, socket.SOCK_STREAM);
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1);
s.bind(('',PORT));
s.listen(1);
conn, addr = s.accept();
print 'Connected by' , addr;
data_addr = list();
while 1:
        data = conn.recv(4096);
        if not data: break;
        data_addr = pickle.loads(data);
        print 'Received Data',  repr(data_addr);
        print data_addr;
        data_addr.pop(0);
        data_addr.pop(0);
        print data_addr;
        data_string1 = pickle.dumps(data_addr);
        s.send(data_string1);
        break;
conn.close();
socket.shutdown();
socket.close();

The entire error msg is:

Traceback (most recent call last):
File "server.py", line 22, in <module>
s.send(data_string1);
socket.error: [Errno 32] Broken pipe

How do I fix this problem so that the client can receive the edited list from the server without any error ? Thank You in advance.

ArnabC
  • 181
  • 1
  • 3
  • 16
  • Please include the whole error traceback to indicate exactly where the message is coming from. – cdarke Dec 07 '16 at 10:26
  • Traceback (most recent call last): File "server.py", line 22, in s.send(data_string1); socket.error: [Errno 32] Broken pipe This is the entire error msg. – ArnabC Dec 07 '16 at 10:32
  • OK, more than likely the client has dropped through the `recv` and closed the socket. You probably need to trace the client (put some `print` statements in) to see why it is closing the socket prematurely. See also http://stackoverflow.com/questions/11866792/how-to-prevent-errno-32-broken-pipe – cdarke Dec 07 '16 at 10:35

1 Answers1

14

You made a small mistake:

s.send(data_string1);

Should be:

conn.send(data_string1);

Also the following lines need to be changed:

socket.shutdown(); to s.shutdown();

And:

socket.close(); to s.close();

Stan Vanhoorn
  • 526
  • 1
  • 4
  • 18