C – 使用poll在socket(s)和stdin之间进行多路复用 – 服务器

我正在编写一个客户端服务器应用程序,我正在使用poll来在多个客户端套接字和stdin之间进行多路复用,我可以在其中插入命令(例如:停止服务器)。 我相信我的代码的结构(“逻辑”)是正确的,但它的行为并不像我期望的那样:

struct pollfd pfd[NSERVER]; //defined as 10 pfd[0].fd = fileno(stdin); pfd[0].events = POLLIN; pfd[1].fd = socktfd; //server bind, listen socket pfd[1].events = POLLIN; struct sockaddr_storage remoteaddr; // client address socklen_t addrlen; char remoteIP[INET6_ADDRSTRLEN]; addrlen = sizeof remoteaddr; char buf[1024]; // buffer int pos=2; while(poll(pfd,1,0) >= 0) { if(pfd[0].revents & POLLIN) { //stdin //process input and perform command } if(pfd[1].revents & POLLIN) { /* new connection */ int connsockfd = accept(socktfd, (struct sockaddr *)&remoteaddr,&addrlen); pfd[pos].fd=connsockfd; } int i=2; //Loop through the fd in pfd for events while (i<=NSERVER) { if (pfd[i].revents & POLLIN) { int c=recv(pfd[i].fd, buf, sizeof buf, 0); if(c<=0) { if (c==0) { /* Client closed socket */ close(pfd[i].fd); } }else {//Client sent some data c=send(pfd[i].fd,sbuff,z,0); if (c<=0) { Error; } free(sbuff); } } i++; } } 

我删除了recv中的一些代码并发送以使代码更容易阅读。 它无法表现(它只是挂起,不接受连接或对stdin的输入做出反应)。

注意:我更倾向于使用民意调查,所以请不要指向选择:-)。

在此先感谢您的任何帮助。

  1. 你应该设置每个pfd[i].fd = -1 ,所以它们最初会被poll()忽略。
  2. poll(pfd, 1, 0)是错误的,至少应该是poll(pfd, 2, 0)或者甚至是poll(pfd, NSERVER, 0)
  3. while(i<=NSERVER)应该是while(i

您的程序可能会挂起,因为您循环遍历pfd数组,该数组未初始化并包含.fd和.revents的随机值,因此它希望在可能阻塞的某个随机FD上发送()或recv()。 做if(pdf[i].fd < 0) {i++; continue;} if(pdf[i].fd < 0) {i++; continue;}i循环中。

您也不要在新接受的套接字上设置pfd[pos].events = POLLIN 。 除非你有东西要发送,否则不要设置POLLOUT ,因为它几乎每次都会触发。