码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-22 16:38:23      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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服务器

标签:

原文地址:http://www.cnblogs.com/chengyuanchun/p/4829090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!