本文介绍了socket.error:[Errno 32]管道损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个客户端-服务器python程序,其中客户端将列表发送到服务器,服务器接收数组,删除列表的前两个元素,然后将其发送回客户端.服务器接收列表没有问题.但是,当服务器要发回编辑后的列表时,它显示错误:socket.error: [Errno 32] Broken pipe.client.py和server.py是从具有不同ip的不同计算机上运行的.我在下面发布了client.py和server.py的代码:

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

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:

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();

整个错误消息为:

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.

推荐答案

您犯了一个小错误:

s.send(data_string1);

应该是:

conn.send(data_string1);

还需要更改以下几行:

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

并且:

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

这篇关于socket.error:[Errno 32]管道损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 12:44