当前位置: 主页 > 日志 > Python >

When does socket.recv(recv_size) return?

From test, i concluded that in following three cases the socket.recv(recv_size) will return.

  • 1) After the connection was closed(for example, the client side called socket.close()) or any socket error occurred, it would return empty string.
  • 2) Some data come, the size of data is more than recv_size.
  • 3) Some data come, the size of data is less than recv_size and no more data come after a short time(i found 0.1s would work)?

More details about 3):

#server.py

while True:
    data
= sock.recv(10)
   
print data, 'EOF'

#client1.py

sock
.sendall("12345")
sock
.sendall("a" * 50)

#client2.py

sock
.sendall("12345")
time
.sleep(0.1)
sock
.sendall("a" * 50)

When i run client1.py, the server.py echos:

12345aaaaa EOF
aaaaaaaaaa EOF
aaaaaaaaaa EOF
aaaaaaaaaa EOF
aaaaaaaaaa EOF
aaaaa EOF

When i run client2.py, the server.py echos:

12345 EOF
aaaaaaaaaa EOF
aaaaaaaaaa EOF
aaaaaaaaaa EOF
aaaaaaaaaa EOF
aaaaaaaaaa EOF

Is my conclusion correct? Where can i see the official description about 3)?

http://stackoverflow.com/questions/7174927/when-does-socket-recvrecv-size-return

 

Some related knowledge:

docs.python.org/library/socket.html#socket.socket.recv

http://linux.die.net/man/3/recv

http://gnosis.cx/publish/programming/sockets.html

 

socket.setblocking(flag) 

Set blocking or non-blocking mode of the socket: if flag is 0, the socket is set to non-blocking, else to blocking mode. Initially all sockets are in blocking mode. In non-blocking mode, if a recv() call doesn’t find any data, or if a send() call can’t immediately dispose of the data, a error exception is raised; in blocking mode, the calls block until they can proceed. s.setblocking(0) is equivalent to s.settimeout(0); s.setblocking(1) is equivalent to s.settimeout(None). 

 

 

[日志信息]

该日志于 2011-08-24 19:53 由 redice 发表在 redice's Blog ,你除了可以发表评论外,还可以转载 “When does socket.recv(recv_size) return?” 日志到你的网站或博客,但是请保留源地址及作者信息,谢谢!!    (尊重他人劳动,你我共同努力)
   
验证(必填):   点击我更换验证码

redice's Blog  is powered by DedeCms |  Theme by Monkeii.Lee |  网站地图 |  本服务器由西安鲲之鹏网络信息技术有限公司友情提供

返回顶部