[英]Python socket.send() can only send once, then socket.error: [Errno 32] Broken pipe occurred
我是網絡編程的新手,所以如果這是一個愚蠢的問題,請原諒我:) 我使用 Python2.7 在 Ubuntu 10.04.2 上創建了 1 個客戶端和 1 個 SocketServer.ThreadingMixIn 服務器,但似乎我只能調用 sock .send() 一次在客戶端,然后我會得到:
Traceback (most recent call last):
File "testClient1.py", line 33, in <module>
sock.send('c1:{0}'.format(n))
socket.error: [Errno 32] Broken pipe
這是我寫的代碼:
測試客戶端1.py:
#! /usr/bin/python2.7
# -*- coding: UTF-8 -*-
import sys,socket,time,threading
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
sock.connect(('localhost',20000))
except socket.error:
print('connection error')
sys.exit(0)
n=0
while n<=1000:
sock.send('c1:{0}'.format(n))
result=sock.recv(1024)
print(result)
n+=1
time.sleep(1)
測試服務器.py:
#! /usr/bin/python2.7
# -*- coding: UTF-8 -*-
import threading,SocketServer,time
class requestHandler(SocketServer.StreamRequestHandler):
#currentUserLogin={} #{clientArr:accountName}
def handle(self):
requestForUpdate=self.rfile.read(4)
print(requestForUpdate)
self.wfile.write('server reply:{0}'.format(requestForUpdate))
class broadcastServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
if __name__ == '__main__':
server=broadcastServer(('localhost',20000),requestHandler)
t = threading.Thread(target=server.serve_forever)
t.daemon=True
t.start()
print('server start')
n=0
while n<=60:
print(n)
n+=1
time.sleep(1)
server.socket.close()
我在 2 個單獨的終端中運行它們:
第一個端子的 output:
$ python2.7 testServer.py
server start
0
1
2
3
4
c1:0
5
6
7
8
9
10
11
...
第 2 端子 output:
$ python2.7 testClient1.py
server reply:c1:0
Traceback (most recent call last):
File "testClient1.py", line 33, in <module>
sock.send('c1:{0}'.format(n))
socket.error: [Errno 32] Broken pipe
我嘗試在 testClient.py 中直接調用 sock.send() 兩次,例如:
while n<=1000:
sock.send('c1:{0}'.format(n))
sock.send('12333')
result=sock.recv(1024)
print(result)
n+=1
time.sleep(1)
但是終端的輸出仍然是一樣的:(誰能指出我在這里做錯了什么?謝謝!
這是我想出的[Sol]。 謝謝馬克:)
測試客戶端1.py:
import sys,socket,time
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
sock.connect(('localhost',20000))
except socket.error:
print('connection error')
sys.exit(0)
n=0
while n<=10: #connect once
sock.send('c1:{0}'.format(n))
result=sock.recv(1024)
print(result)
n+=1
time.sleep(1)
sock.close()
#once you close a socket, you'll need to initialize it again to another socket obj if you want to retransmit
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
sock.connect(('localhost',20000))
except socket.error:
print('connection error')
sys.exit(0)
n=0
while n<=10: #connect once
sock.send('c3:{0}'.format(n))
result=sock.recv(1024)
print(result)
n+=1
time.sleep(1)
sock.close()
測試服務器.py:
import threading,SocketServer,time
class requestHandler(SocketServer.StreamRequestHandler):
#currentUserLogin={} #{clientArr:accountName}
def handle(self):
requestForUpdate=self.request.recv(1024)
print(self.client_address)
while requestForUpdate!='':
print(requestForUpdate)
self.wfile.write('server reply:{0}'.format(requestForUpdate))
requestForUpdate=self.request.recv(1024)
print('client disconnect')
class broadcastServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
if __name__ == '__main__':
server=broadcastServer(('localhost',20000),requestHandler)
t = threading.Thread(target=server.serve_forever)
t.daemon=True
t.start()
print('server start')
n=0
while n<=60:
print(n)
n+=1
time.sleep(1)
server.socket.close()
對於每個連接,handle() 在SocketServer.StreamRequestHandler
中調用一次。 如果您從handle
返回,則連接將關閉。
如果您希望服務器處理多個發送/接收,則必須循環直到 recv() 返回 0,表示客戶端關閉了連接(或至少在發送時調用了 shutdown())。
另請注意,TCP 是流式協議。 您需要設計一個消息協議來指示消息的長度或結尾,並緩沖recv
直到獲得完整的消息。 檢查send
返回值以確保所有消息都已發送,或使用sendall
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.