epoll-创新互联
#include "local.h" #include "utils.h" using namespace std; // 存放客户端socket描述符的list list目前成都创新互联公司已为数千家的企业提供了网站建设、域名、网站空间、网站托管维护、企业网站设计、巴彦淖尔网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。clients_list; int main(int argc, char *argv[]) { int listener; //监听socket struct sockaddr_in addr, their_addr; addr.sin_family = PF_INET; addr.sin_port = htons(SERVER_PORT); addr.sin_addr.s_addr = inet_addr(SERVER_HOST); socklen_t socklen; socklen = sizeof(struct sockaddr_in); static struct epoll_event ev, events[EPOLL_SIZE]; ev.events = EPOLLIN | EPOLLET; //对读感兴趣,边沿触发 char message[BUF_SIZE]; int epfd; //epoll描述符 clock_t tStart; //计算程序运行时间 int client, res, epoll_events_count; CHK2(listener, socket(PF_INET, SOCK_STREAM, 0)); //初始化监听socket setnonblocking(listener); //设置监听socket为不阻塞 CHK(bind(listener, (struct sockaddr *)&addr, sizeof(addr))); //绑定监听socket CHK(listen(listener, 1)); //设置监听 CHK2(epfd,epoll_create(EPOLL_SIZE)); //创建一个epoll描述符,并将监听socket加入epoll ev.data.fd = listener; CHK(epoll_ctl(epfd, EPOLL_CTL_ADD, listener, &ev)); //把被监听的fd和监听的事件放入epoll的文件描述符中 // while(1) { CHK2(epoll_events_count,epoll_wait(epfd, events, EPOLL_SIZE, EPOLL_RUN_TIMEOUT)); tStart = clock(); for(int i = 0; i < epoll_events_count ; i++) { if(events[i].data.fd == listener) //新的连接到来,将连接添加到epoll中,并发送欢迎消息 { CHK2(client,accept(listener, (struct sockaddr *) &their_addr, &socklen)); setnonblocking(client); ev.data.fd = client; CHK(epoll_ctl(epfd, EPOLL_CTL_ADD, client, &ev)); clients_list.push_back(client); // 添加新的客户端到list bzero(message, BUF_SIZE); res = sprintf(message, STR_WELCOME, client); CHK2(res, send(client, message, BUF_SIZE, 0)); }else { CHK2(res,handle_message(events[i].data.fd)); //注意:这里并没有调用epoll_ctl重新设置socket的事件类型,但还是可以继续收到客户端发送过来的信息 } } printf("Statistics: %d events handled at: %.2f second(s)\n", epoll_events_count, (double)(clock() - tStart)/CLOCKS_PER_SEC); } close(listener); close(epfd); return 0; } int handle_message(int client) { char buf[BUF_SIZE], message[BUF_SIZE]; bzero(buf, BUF_SIZE); bzero(message, BUF_SIZE); int len; CHK2(len,recv(client, buf, BUF_SIZE, 0)); //接受客户端信息 if(len == 0) //客户端关闭或出错,关闭socket,并从list移除socket { CHK(close(client)); clients_list.remove(client); } else //向客户端发送信息 { if(clients_list.size() == 1) { CHK(send(client, STR_NOONE_CONNECTED, strlen(STR_NOONE_CONNECTED), 0)); return len; } sprintf(message, STR_MESSAGE, client, buf); list ::iterator it; for(it = clients_list.begin(); it != clients_list.end(); it++) { if(*it != client) { CHK(send(*it, message, BUF_SIZE, 0)); } } } return len; }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前题目:epoll-创新互联
网页路径:http://scyanting.com/article/cscpoo.html