关于socket阻塞与非阻塞情况下的recv、send、read、write返回值

Ryan posted @ 2010年8月25日 22:09 in 网文摘抄 , 3225 阅读

原文地址:http://blog.csdn.net/eroswang/archive/2010/06/02/5642550.aspx

 1、阻塞模式与非阻塞模式下recv的返回值各代表什么意思?有没有 区别? 

(就我目前了解阻塞与非阻塞recv返回值没有区分,都是 <0:出错,=0:连接关闭,>0接收到数据大小,特别:返回值 < 0时并且(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)的情况下认为连接是 正常的,继续接收。只是阻塞模式下recv会阻塞着接收数据,非阻塞模式下如果没有数据会返回,不会阻塞着读,因此需要循环读取。

 2、阻塞模式与非阻塞模式下write的返回值各代表什么意思? 有没有区别? 
  

 阻塞与非阻塞write返回值没有区分,都是 <0:出错,=0:连接关闭,>0发送数据大小,特别:返回值 <0时并且 (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)的情况下认为连接是正常的, 继续发送。只是阻塞模式下write会阻塞着发送数据,非阻塞模式下如果暂时无法发送数据会返回,不会阻塞着 write,因此需要循环发送。    

 3、阻塞模式下read返回 值 < 0 && errno != EINTR && errno != EWOULDBLOCK & amp;& errno != EAGAIN时,连接异常,需要关闭,read返回值 < 0 && amp; (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)时表示没有数据, 需要继续接收,如果返回值大于0表示接送到数据。     

非阻塞模式下read返回值 < 0表示没有数据,= 0表示 连接断开,> 0表示接收到数据。

这2种模式下的返回值是不是这么理解,有没有更详细的理解或更准确的说明?    

4、阻塞模式与非阻塞模式下是否send返回 值 < 0 && (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) 表示暂时发送失败,需要重试,如果send返回值 <= 0, && errno != EINTR && amp; errno != EWOULDBLOCK && errno != EAGAIN时,连接异常,需要关闭,如果send返回 值 > 0则表示发送了数据?send的返回值是否这么理解,阻塞模式与非阻塞模式下send返回值=0是否都是发送失败,还是那个模式下表示暂时 不可发送,需要 重发?

Avatar_small
NCERT Question Paper 说:
2022年9月29日 20:11

NCERT Sample Paper 2023 Pdf Download for 1st, 2nd, 3rd, 4th, 5th, 7th, 8th, 9th, 10th, 11th & 12th Standard Sample Pape Suggestions for Hindi Medium, English Medium & Urdu Medium students studying at CBSE, KVS, NCERT Question Paper Pdf Download JNV and other Central & State Education Boards of the Countr.NCERT Sample Paper 2023 Pdf Download for 1st, 2nd, 3rd, 4th, 5th, 7th, 8th, 9th, 10th, 11th & 12th Standard Sample Pape Suggestions for Hindi Medium, English Medium & Urdu Medium students studying at CBSE, KVS, JNV and other Central & State Education Boards of the Country.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter