- 原因一: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。
IO复用要设置socket FD非阻塞的原因
最新推荐文章于 2024-06-08 23:48:45 发布