[問題] 請問socket send什麼時候會回傳0
根據 Socket Programming HOWTO
if a socket send or recv returns after handling 0 bytes, the connection has
been broken.
但是我試不出來什麼時候 send 會回傳 0
不管用 close, shutdown, 還是直接關掉程式
全都 raise socket.error
請問什麼情況下 send 會回傳 0 呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.46.147.61
→
05/09 07:58, , 1F
05/09 07:58, 1F
→
05/09 08:01, , 2F
05/09 08:01, 2F
可能沒說清楚,我是想寫類似 multiprocessing.Connection 的東西
所以想知道發生錯誤時 multiprocessing.Connection 會怎麼處理
比如說 recv 到空字串的時候,multiprocessing.Connection 會 raise EOFError
這在一邊已經 close socket,另一邊仍繼續使用 recv 時會發生
但我找不出方法讓 send 回傳 0
所以沒辦法知道 multiprocessing.Connection 在這情況下會有什麼動作
document 也沒寫清楚,只好用測試的 ...
→
05/09 17:19, , 3F
05/09 17:19, 3F
→
05/09 17:20, , 4F
05/09 17:20, 4F
→
05/09 17:20, , 5F
05/09 17:20, 5F
→
05/09 17:21, , 6F
05/09 17:21, 6F
→
05/09 17:43, , 7F
05/09 17:43, 7F
→
05/09 17:51, , 8F
05/09 17:51, 8F
→
05/09 17:52, , 9F
05/09 17:52, 9F
因為看範例這樣寫:
def mysend(self, msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError("socket connection broken")
totalsent = totalsent + sent
才以為 send 回傳 0 是代表有錯誤發生
但是就算是傳空字串,也不可能跑到範例的 while 迴圈裡
所以這範例是有點小錯誤的嗎?問題很蠢請見諒
※ 編輯: os653 來自: 114.46.147.61 (05/10 01:43)
Python 近期熱門文章
PTT數位生活區 即時熱門文章