如何使用select()获得poll()的POLLHUP等价物?

时间:2014-09-15 15:17:53

标签: c++ c windows mingw

我目前正在将一些代码从Linux移植到Windows(使用MinGW)。

根据understand,MinGW不支持原始版本中使用的poll(),因此我正在重写select()的所有内容。

现在我偶然发现了if (pfd[i].revents & (POLLERR|POLLHUP)) ......

如何使用select()获得相应的条件 - 或者使用winsock api或MinGW提供的任何内容? POLLERR部分很简单; if(FD_ISSET (i, &error_fd_set))但我对POLLHUP部分感到茫然。

2 个答案:

答案 0 :(得分:3)

你不能。你必须使用"普通"找出连接是否已关闭的方法,即从中读取连接 就代码而言,它是:

int rc = select(max_fd + 1, read_set, ..., ..., ...);
// check rc
for (int i = 0; i <= max_fd; ++i) {
  if (FD_ISSET(i, &read_set)) { // data incoming on i
     int rc = read(i, ..., ...); // or recv, if you use some flag
     if (rc == 0) {
        // i hung up
    }
  }
}

否则,您可以使用WSAPoll提供类似于您在类UNIX系统中所期望的API。

有关WSAPoll的更多信息。

答案 1 :(得分:3)

根据我的The Linux Programming Interface副本,内核轮询事件映射到select()事件,如下所示:

// Ready for reading
#define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
// Ready for writing
#define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
// Exceptional condition
#define POLLEX_SET (POLLPRI)

因此,这表明您需要检查“准备好”&#39;事件。要真正区分POLLHUP,POLLIN和POLLIN | POLLHUP,您可以使用本书中的以下图表:

| ----- Condition or event ---- |
Data in pipe? | Write end open? | select() | poll()
no              no                r        | POLLHUP
yes             yes               r        | POLLIN
yes             no                r        | POLLIN | POLLHUP