本文介绍了套接字选择()与非块接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些比较 select()poll()epoll() 的文章,我已经看过许多讨论 select() 与多个套接字的实际用法的指南.

I've seen a few write-ups comparing select() with poll() or epoll(), and I've seen many guides discussing the actual usage of select() with multiple sockets.

然而,我似乎无法找到与没有 select() 的非阻塞 recv() 调用的比较.如果只有 1 个要读取的套接字和 1 个要写入的套接字,是否有任何理由使用 select() 调用?recv() 方法可以设置为在没有可用数据时不阻塞并返回错误(WSAEWOULDBLOCK),那么何必调用 select() 当您没有其他套接字要检查时?非阻塞 recv() 调用会慢很多吗?

However, what I can't seem to find is a comparison to a non-blocking recv() call without select(). In the event of only having 1 socket to read from and 1 socket to write to, is there any justification for using the select() call? The recv() method can be setup to not block and return an error (WSAEWOULDBLOCK) when there is no data available, so why bother to call select() when you have no other sockets to examine? Is the non-blocking recv() call much slower?

推荐答案

当您轮询无限消耗 CPU 时间时,您不希望在没有其他方法的情况下对 recv 进行非阻塞调用以等待套接字上的数据.

You wouldn't want a non-blocking call to recv without some other means for waiting for data on the socket as you poll infinitely eating up cpu time.

如果您没有其他套接字要检查,并且在同一线程中没有其他事情可做,则对 read 的阻塞调用可能是最有效的解决方案.虽然在这种情况下,考虑到这样的效率,就像是过早的优化.

If you have no other sockets to examine and nothing else to do in the same thread, a blocking call to read is likely to be the most efficient solution. Although in such a situation, considering the efficiency of this is like to be premature optimisation.

这些考虑因素只会随着套接字数量的增加而发挥作用.

These kinds of considerations only tend to come into play as the socket count increases.

非阻塞调用只会在单个线程上处理多个套接字的上下文中更快.

Nonblocking calls are only faster in the context of handling multiple sockets on a single thread.

这篇关于套接字选择()与非块接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:29