作者os653 (allstar)
看板Python
标题[问题] 请问socket send什麽时候会回传0
时间Wed May 8 20:15:11 2013
根据 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
2F:→ AndCycle:Returns the number of bytes sent. 05/09 08:01
可能没说清楚,我是想写类似 multiprocessing.Connection 的东西
所以想知道发生错误时 multiprocessing.Connection 会怎麽处理
比如说 recv 到空字串的时候,multiprocessing.Connection 会 raise EOFError
这在一边已经 close socket,另一边仍继续使用 recv 时会发生
但我找不出方法让 send 回传 0
所以没办法知道 multiprocessing.Connection 在这情况下会有什麽动作
document 也没写清楚,只好用测试的 ...
3F:→ AndCycle:document写的已经很清楚了, 他的return value就是这个, 05/09 17:19
4F:→ AndCycle:所有有错误的情况都会另外raise exception, 05/09 17:20
6F:→ AndCycle:如果你想要0那麻烦请你自己包装起来处理 05/09 17:21
8F:→ AndCycle:s.send('') 你就会得到你的零, 你为什麽会想要零呢 ... 05/09 17:51
9F:→ AndCycle:还有你quote的那句话并不是跟你说他会回传零 =_= 05/09 17:52
因为看范例这样写:
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)