Unix网络编程-poll模型echo服务器

poll函数和select函数差不多。以下是一个简单的回显服务器



#include <iostream> using namespace std; #include <poll.h> #include <limits.h> #define OPEN_MAX 64 int main() { int i, maxi, listenfd, connfd, sockfd; int nready; ssize_t n; socklen_t clilen; struct pollfd client[OPEN_MAX]; struct sockaddr_in cliaddr, servaddr; listenfd = socket(AF_INET, SOCK_STREAM, 0); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(10002); servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); if(bind(listenfd, (sockaddr*)&servaddr, sizeof(servaddr)) == -1) { cout << "bind error" << endl; return -1; } listen(listenfd, 5); client[0].fd = listenfd; client[0].events = POLLRDNORM; for(int i = 1; i < OPEN_MAX; i++) { client[i].fd = -1; } maxi = 0; cout << "startup sucess port : " << servaddr.sin_port << endl; cout << "Max connect :" << OPEN_MAX << endl; for(;;) { nready = poll(client, maxi + 1, -1); if(nready == -1) { cout << "poll error" << endl; } if(client[0].revents & POLLRDNORM)//new client connection { clilen = sizeof(cliaddr); connfd = accept(listenfd, (sockaddr*)&cliaddr, &clilen); for(i = 1; i < OPEN_MAX; i++) { if(client[i].fd < 0) { client[i].fd = connfd; //save des break; } } if(i == OPEN_MAX) { cout << "too many clients" << endl; return -1; } client[i].events = POLLRDNORM; if(i > maxi) { maxi = i; } if(--nready <= 0) { continue; } } for(i = 1; i <= maxi; i++) { if((sockfd = client[i].fd) < 0) { continue; } if(client[i].revents & (POLLRDNORM | POLLERR)) { char buffer[1024] = {0}; if((n = recv(sockfd, buffer, 1024, 0)) < 0) { if(errno == ECONNRESET) { //connection reset by client close(sockfd); client[i].fd = -1; } else { cout << "read error" << endl; } } else if(n == 0) { //connect close by client close(sockfd); client[i].fd = -1; } else { send(sockfd, buffer, 1024, 0); } if(--nready <= 0) { break; } } } } return 0; }

 

你可能感兴趣的:(Unix网络编程-poll模型echo服务器)