[問題] 請問socket send什麼時候會回傳0

看板Python作者 (allstar)時間12年前 (2013/05/08 20:15), 編輯推噓0(009)
留言9則, 1人參與, 最新討論串1/1
根據 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 08:01, , 2F
Returns the number of bytes sent.
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
document寫的已經很清楚了, 他的return value就是這個,
05/09 17:19, 3F

05/09 17:20, , 4F
所有有錯誤的情況都會另外raise exception,
05/09 17:20, 4F

05/09 17:20, , 5F
http://www.manpagez.com/man/2/send/ 他只是跟著底層做
05/09 17:20, 5F

05/09 17:21, , 6F
如果你想要0那麻煩請你自己包裝起來處理
05/09 17:21, 6F

05/09 17:51, , 8F
s.send('') 你就會得到你的零, 你為什麼會想要零呢 ...
05/09 17:51, 8F

05/09 17:52, , 9F
還有你quote的那句話並不是跟你說他會回傳零 =_=
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)
文章代碼(AID): #1HYa7Hrm (Python)
文章代碼(AID): #1HYa7Hrm (Python)