我想知道在轮询设置这些位时应该怎么做?关闭套接字,忽略它或什么?
这项轮询业务似乎是由疯子编写的,我不确定如何使用它来允许多个客户端连接到服务器,然后将其输入发送给所有其他客户端。
因此,如果我想让三个客户加入,我将需要以下内容:
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)
然后到底该做些什么以便可以读入和写出消息?
我写了一个小测试程序来弄清楚如何交谈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) 该联机帮助页用于民意调查(2):
POLLHUP - 挂机(仅限输出)
POLLNVAL - 无效请求:fd未打开(仅输出)
究竟有什么区别?编写一个简单的程序会显示POLLNVAL如果我关闭文件描述符将触发,然后尝试从关闭的fd读取.但是,我无法弄清楚返回的方法POLLHUP.