什么时候使用poll C函数的POLLOUT事件? [英] When to use the POLLOUT event of the poll C function?

查看:507
本文介绍了什么时候使用poll C函数的POLLOUT事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用socket() + POLLIN poll() + recv() + send()编写了一个小型TCP服务器,但是我不知道何时使用 POLLOUT 轮询或选择writefds来对可写事件进行轮询.

I wrote a small TCP servers with socket() + POLLIN poll() + recv() + send(), but I don't know when to use POLLOUT poll or select writefds to poll on writable event.

有人可以给我一个 POLLOUT 的实际用法示例吗?

Can anyone give me an example of the real usage of POLLOUT?

推荐答案

通常的模式是将非阻塞文件描述符与poll()一起使用,如下所示:

The usual pattern is to use non-blocking file descriptors with poll() like this:

  • 准备好poll()时,
    • 始终设置POLLIN是因为您始终对读取套接字另一端发送给您的内容感兴趣.
      • 除非您有大量积压的传入数据,并且您有意让另一端在发送更多数据之前等待.
      • When getting ready to poll(),
        • Always set POLLIN because you are always interested in reading what the other end of the socket has send you.
          • Except if you have a large backlog of incoming data and you intentionally want to make the other end wait before sending more.
          • 阅读并使用它
          • 尝试发送未完成的数据.
            • 如果您成功编写了所有内容,则下次无需在循环中设置POLLOUT
            • 如果您仅设法发送了其中一部分(或全部未发送),请保留其余部分以备后用.您将在下一次循环中设置POLLOUT.
            • Try sending your outstanding data.
              • If you managed to write all of it, you're not going to set POLLOUT next time through the loop
              • If you only managed to send some of it (or none of it) then keep the rest for later. You will set POLLOUT the next time through the loop.
              • 强烈尝试立即发送其中的一些邮件.您可能没有成功发送任何,全部或全部.就像前面的情况一样,保留下一次未写入的数据部分,并计划仅在剩余一些数据的情况下才通过循环设置下一次POLLOUT.
              • 只需保留数据,并计划下一次在循环中设置POLLOUT. (这种选择通常更容易编程,因为您只需要在循环中的一个位置处理数据,而另一方面,它将数据写入延迟到下一次循环中.)
              • Eagerly try to send some of it right away. You may successfully send none, some, or all of it. Just like with the previous case, keep the portion of the data that wasn't written for next time and plan to set POLLOUT the next time through the loop only if there was some data left.
              • Just keep a hold on the data and plan to set POLLOUT the next time through the loop. (This choice is often easier to program because you only need to handle writing data in one place in your loop but on the other hand it delays writing the data until the next time through the loop.)

              这篇关于什么时候使用poll C函数的POLLOUT事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆