2016-08-21 182 views
2

我对套接字编程相当陌生,我试图编写一个程序来获得传入的TCP连接并以某种方式管理它们。我想不通,为什么下面的代码是给我一个“人头错误”:什么导致poll()函数,C++错误?

int main(int argc, char *argv[]) { 
char *port; 
struct pollfd connections[MAX_CONNECTIONS]; 
struct addrinfo addr_hints, *addr_result; 
int ret, i; 

for (i = 0; i < MAX_CONNECTIONS; ++i) { 
    connections[i].fd = -1; 
    connections[i].events = POLLIN; 
    connections[i].revents = 0; 
} 

port = "0"; 

memset(&addr_hints, 0, sizeof(struct addrinfo)); 
addr_hints.ai_flags = AI_PASSIVE; 
addr_hints.ai_family = AF_UNSPEC; 
addr_hints.ai_socktype = SOCK_STREAM; 
addr_hints.ai_protocol = IPPROTO_TCP; 

getaddrinfo(NULL, port, &addr_hints, &addr_result); 

connections[0].fd = socket(addr_result->ai_family, addr_result->ai_socktype, addr_result->ai_protocol); 

if (connections[0].fd < 0) { 
    cerr << "Socket error" << endl; 
    return 0; 
} 

if (bind(connections[0].fd, addr_result->ai_addr, addr_result->ai_addrlen) < 0) { 
    cerr << "Bind errror" << endl; 
    return 0; 
} 

if (listen(connections[0].fd, 25) < 0) { 
     cerr << "Listen error" << endl; 
    return 0; 
} 

do { 
    for (i = 0; i < MAX_CONNECTIONS; ++i) 
      connections[i].revents = 0; 

    ret = poll(connections, MAX_CONNECTIONS, -1); 

    if (ret < 0) { 
     cerr << "Poll error" << endl; 
     return 0; 
    } else { 
     //DO SOMETHING 
    } 

} while(true); 
} 

MAX_CONNECTIONS是一个常数设置为10000连接[0]应该是关于这一点我正在听的描述符传入连接。我将端口设置为“0”,因为我想选择一个随机端口。似乎轮询函数立即失败,产生消息“轮询错误”(所以poll()基本上小于0)。我已经检查并在轮询和绑定连接[0]后有一个文件描述符。我不知道我在做什么错,是getaddrinfo函数的东西吗?

+4

当你从系统收到一个错误叫你需要检查['errno'](http://man7.org /linux/man-pages/man3/errno.3.html)来获取错误代码。您可以使用例如['strerror'](http://en.cppreference.com/w/cpp/string/byte/strerror)获取可打印的错误字符串。 –

+0

我明白了。我按你的建议做了,结果证明这是一个“无效参数”错误。对于pollfd结构的大小是否有某种限制?我不知道这里还有什么可能是错的。 – TheMountainThatCodes

回答

3

问题是您的轮询文件描述符数组太大。它的最大尺寸可以定义为RLIMIT_NOFILE。对于您的系统,这可能是1024。将MAXIMUM_CONNECTIONS降至此值或更低。

从调查规格:

EINVAL The nfds value exceeds the RLIMIT_NOFILE value. 

多见于:http://man7.org/linux/man-pages/man2/poll.2.html