IO复用要设置socket FD非阻塞的原因

  • 原因一:IO复用的局限:
    man select:Under Linux, select() may report a socket file descriptor as “ready for reading”, while nevertheless a subsequent read blocks. This could for example happen when data has arrived but upon examination has wrong checksum and is discarded. There may be other circumstances in which a file descriptor is spuriously reported as ready. Thus it may be safer to use O_NONBLOCK on sockets that should not block.
    即可能存在的情况:有数据到达select返回可读,但是数据的校验和错误数据被丢弃,但是read会被阻塞
  • 原因二:可读数据的长度未知
    IO复用只返回可读,但是由于可读数据的长度未知,可能读一次无法读完,阻塞IO无法判断是否已经读完数据,因为再次读可能会陷入阻塞,非阻塞IO能够使用while(read)进行读,直到返回EWOULDBLOCK/EAGAIN。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值