标签: poll-syscall

如何处理Linux套接字会重现POLLERR,POLLHUP和POLLNVAL?

我想知道在轮询设置这些位时应该怎么做?关闭套接字,忽略它或什么?

sockets linux network-programming poll-syscall

13
推荐指数
3
解决办法
2万
查看次数

如何使用民意调查来接受多个客户?(TCP服务器)(C)

这项轮询业务似乎是由疯子编写的,我不确定如何使用它来允许多个客户端连接到服务器,然后将其输入发送给所有其他客户端。

因此,如果我想让三个客户加入,我将需要以下内容:

  ufds[0].fd = sd;
  ufds[0].events = POLLIN;
  ufds[1].fd = sd2;
  ufds[1].events = POLLOUT;
  ufds[2].fd = sd2;
  ufds[2].events = POLLOUT;
  ufds[3].fd = sd2;
  ufds[3].events = POLLOUT;
  ufds[4].fd = sd2;
  ufds[4].events = POLLOUT;
Run Code Online (Sandbox Code Playgroud)

然后到底该做些什么以便可以读入和写出消息?

c tcp chat client-server poll-syscall

6
推荐指数
1
解决办法
1万
查看次数

尽管没有输入,为什么民意调查会继续返回?

我写了一个小测试程序来弄清楚如何交谈poll.我创建了三个文件testa,testb,testc和写入字符串hello\n到第一.所以,这是我的调用poll:

poll(polls.data(),polls.size(),-1)
Run Code Online (Sandbox Code Playgroud)

根据联机帮助页,超时-1应该表明系统调用永远不会超时.但是,它不停地返回而没有任何东西可读.我总是消耗输入的一个字节,可以看到hello\n正在打印,但民意调查不止于此.它只是一直假装有东西要读.

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/poll.h>
#include <unistd.h>
#include <errno.h>

#include <vector>
#include <map>
#include <string>
#include <iostream>

typedef int fd_t;

int main() {
  fd_t const a = open("testa",O_RDONLY);
  fd_t const b = open("testb",O_WRONLY);
  fd_t const c = open("testc",O_RDWR);
  std::map<fd_t,std::string> names{{{a,"testa"},{b,"testb"},{c,"testc"}}};

  std::vector<pollfd> polls;
  polls.push_back(pollfd{a, POLLIN, 0});
  polls.push_back(pollfd{b, 0, 0});
  polls.push_back(pollfd{c, POLLIN, 0});

  while (poll(polls.data(),polls.size(),-1)) {
    for (auto p : …
Run Code Online (Sandbox Code Playgroud)

c++ linux file-descriptor poll-syscall

5
推荐指数
1
解决办法
3922
查看次数

POLLHUP与POLLNVAL,或什么是POLLHUP?

该联机帮助页用于民意调查(2):

POLLHUP - 挂机(仅限输出)

POLLNVAL - 无效请求:fd未打开(仅输出)

究竟有什么区别?编写一个简单的程序会显示POLLNVAL如果我关闭文件描述符将触发,然后尝试从关闭的fd读取.但是,我无法弄清楚返回的方法POLLHUP.

c unix pipe poll-syscall

4
推荐指数
2
解决办法
1万
查看次数