epoll实例

来源:互联网 发布:mac桌面所有文件不见了 编辑:程序博客网 时间:2024/06/03 16:25
#include <iostream>#include <sys/socket.h>#include <sys/epoll.h>#include <netinet/in.h>#include <arpa/inet.h>#include <fcntl.h>#include <unistd.h>#include <stdio.h>#include <errno.h>using namespace std;#define MAXLINE 5#define OPEN_MAX 100#define LISTENQ 20#define SERV_PORT 5000#define INFTIM 1000void setnonblocking(int sock){    int opts;    opts=fcntl(sock,F_GETFL);    if(opts<0)    {        perror("fcntl(sock,GETFL)");        exit(1);    }    opts = opts|O_NONBLOCK;    if(fcntl(sock,F_SETFL,opts)<0)    {        perror("fcntl(sock,SETFL,opts)");        exit(1);    }}int main(int argc, char* argv[]){    int i, maxi, listenfd, connfd, sockfd,epfd,nfds, portnumber;    ssize_t n;    char line[MAXLINE];    socklen_t clilen;    if ( 2 == argc ){        if( (portnumber = atoi(argv[1])) < 0 ){            fprintf(stderr,"Usage:%s portnumber\n",argv[0]);            return 1;        }    }    else{        fprintf(stderr,"Usage:%s portnumber\n",argv[0]);        return 1;    }        struct epoll_event ev,events[20];       epfd=epoll_create(256);    struct sockaddr_in clientaddr;    struct sockaddr_in serveraddr;        listenfd = socket(AF_INET, SOCK_STREAM, 0);        setnonblocking(listenfd);    ev.data.fd=listenfd;    ev.events=EPOLLIN|EPOLLET;        //ev.events=EPOLLIN;    epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev);    bzero(&serveraddr, sizeof(serveraddr));    serveraddr.sin_family = AF_INET;    char *local_addr="127.0.0.1";        inet_aton(local_addr,&(serveraddr.sin_addr));    serveraddr.sin_port=htons(portnumber);        bind(listenfd,(sockaddr *)&serveraddr, sizeof(serveraddr));    listen(listenfd, LISTENQ);        maxi = 0;    for ( ; ; )     {        nfds=epoll_wait(epfd,events,20,500);        for(i=0;i<nfds;++i)        {            if(events[i].data.fd==listenfd)            {                connfd = accept(listenfd,(sockaddr *)&clientaddr, &clilen);                if(connfd<0){                    perror("connfd<0");                    exit(1);                }                setnonblocking(connfd);                char *str = inet_ntoa(clientaddr.sin_addr);                cout << "Accept a connection from " << str << endl;                               ev.data.fd=connfd;                ev.events=EPOLLIN|EPOLLET;                //ev.events=EPOLLIN;                epoll_ctl(epfd,EPOLL_CTL_ADD,connfd,&ev);            }            else if(events[i].events&EPOLLIN)            {                cout << "EPOLLIN" << endl;                if ( (sockfd = events[i].data.fd) < 0)                    continue;                if ( (n = read(sockfd, line, MAXLINE)) < 0) {                    if (errno == ECONNRESET) {                        close(sockfd);                        events[i].data.fd = -1;                    }     else                        std::cout<<"readline error"<<std::endl;                } else if (n == 0) {                    close(sockfd);                    events[i].data.fd = -1;                }                line[n] = '/0';                            ev.data.fd=sockfd;                ev.events=EPOLLOUT|EPOLLET;                epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);            }            else if(events[i].events&EPOLLOUT)             {            cout << "EPOLLOUT" << endl;                sockfd = events[i].data.fd;                write(sockfd, line, n);                              ev.data.fd=sockfd;                ev.events=EPOLLIN|EPOLLET;                epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);            }        }    }    return 0;}
#include<stdlib.h>#include<iostream>#include<sys/socket.h>#include<sys/epoll.h>#include<arpa/inet.h>#include<netinet/in.h>#include<unistd.h>#include<errno.h>#include<string.h>#include<stdio.h>#include<fcntl.h>using namespace std;#define MAXLINE 1024#define LISTEN 25#define PORT 5000void setnonblocking(int fd){int ret;ret=fcntl(fd,F_GETFL);if(ret<0){perror("fcntl error");exit(-1);}ret=ret|O_NONBLOCK;if(fcntl(fd,F_SETFL,ret)<0){perror("fcntl error");exit(-1);}}int main(){int sock,conn,epfd,nfds,max;struct sockaddr_in serveraddr,clientaddr;int n,yes=1,i,fd;char line[MAXLINE];socklen_t len;char *localaddr="127.0.0.1";struct epoll_event ev,events[20]; //important defineepfd = epoll_create(256);//memset(serveraddr,0,sizeof(struct sockaddr_in));//memset(clientaddr,0,sizeof(struct sockaddr_in));sock = socket(AF_INET,SOCK_STREAM,0);if(sock<0){perror("socket error");exit(-1);}if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&yes,(socklen_t)(sizeof(yes)))<0){perror("setsockopt eror");exit(-1);}setnonblocking(sock);ev.data.fd = sock;ev.events = EPOLLIN|EPOLLET;  epoll_ctl(epfd,EPOLL_CTL_ADD,sock,&ev);serveraddr.sin_family = AF_INET;serveraddr.sin_port = htons(PORT);inet_aton(localaddr,&(serveraddr.sin_addr));if(bind(sock,(struct sockaddr*)&serveraddr,sizeof(serveraddr))<0){perror("bind error");exit(-1);}if(listen(sock,LISTEN)<0){perror("listen error");exit(-1);}int rc=0;int wc=0;while(1){nfds = epoll_wait(epfd,events,20,500);if(nfds<0){if(errno==EINTR){continue;}}for(i=0;i<nfds;i++){if(events[i].data.fd == sock){cout<<"+++++++++++++++accept+++++++++++++"<<endl;len = sizeof(clientaddr);conn = accept(sock,(struct sockaddr*)&clientaddr,&len);if(conn<0){perror("accept error");exit(-1);}setnonblocking(conn);ev.data.fd = conn;ev.events = EPOLLIN|EPOLLET;epoll_ctl(epfd,EPOLL_CTL_ADD,conn,&ev);}else if(events[i].events&EPOLLIN){//cout<<"+++++++++++++++EPOLLIN++++++++++++++++"<<endl;fd=events[i].data.fd;memset(line,0,MAXLINE);n=read(fd,line,MAXLINE);if(n<0){ev.data.fd = events[i].data.fd;ev.events = EPOLLIN|EPOLLET;epoll_ctl(epfd,EPOLL_CTL_MOD,fd,&ev);continue;}else if(n==0){ev.data.fd = events[i].data.fd;close(fd);epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev);continue;}if(n>0){line[++n]='\0';cout<<"Read:"<<line<<endl;rc++;}ev.data.fd=fd;ev.events = EPOLLOUT|EPOLLET;epoll_ctl(epfd,EPOLL_CTL_MOD,fd,&ev);}else if(events[i].events&EPOLLOUT){//cout<<"+++++++++++EPOLLOUT+++++++++++++"<<endl;int ret;int fd1=events[i].data.fd;ret=write(fd1,line,n+1);if(ret<0){ev.data.fd=events[i].data.fd;ev.events = EPOLLOUT|EPOLLET;epoll_ctl(epfd,EPOLL_CTL_MOD,fd1,&ev);continue;}else if(ret==0){ev.data.fd = events[i].data.fd;close(fd1);epoll_ctl(epfd, EPOLL_CTL_DEL, fd1, &ev);continue;}if(ret>0){cout<<"Write to client:"<<line<<endl;memset(line,0,MAXLINE);}ev.data.fd = fd1;ev.events = EPOLLIN|EPOLLET;epoll_ctl(epfd,EPOLL_CTL_MOD,fd1,&ev);}}}return 0;}

http://blog.csdn.net/ljx0305/article/details/4065058

http://www.cppblog.com/converse/archive/2008/10/12/63836.html


原创粉丝点击