epoll use case

#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>
#include <string.h>
using namespace std;

#define MAXLINE 5
#define OPEN_MAX 100
#define LISTENQ 20
#define SERV_PORT 5000
#define INFTIM 1000

void 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\n",argv[0]);
            return 1;
        }
    }
    else
    {
        fprintf(stderr,"Usage:%s portnumber\n\n",argv[0]);
        return 1;
    }

    //Declare the variables of the epoll_event structure, ev is used to register events, and the array is used to return the events to be processed
    struct epoll_event ev,events[20];
    //Generate epoll-specific file descriptors for accept

    epfd=epoll_create(256);
    struct sockaddr_in clientaddr;
    struct sockaddr_in serveraddr;
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    //Set socket to non-blocking mode

    //setnonblocking(listenfd);

    //Set the file descriptor related to the event to be processed

    ev.data.fd=listenfd;
    //Set the type of event to be processed

    ev.events=EPOLLIN|EPOLLET;
    //ev.events=EPOLLIN;

    //Register epoll event

    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));//htons(portnumber);

    serveraddr.sin_port=htons(portnumber);
    bind(listenfd,(sockaddr *)&serveraddr, sizeof(serveraddr));
    listen(listenfd, LISTENQ);
    maxi = 0;
    for (;;) {         //Waiting for the occurrence of epoll event

        nfds=epoll_wait(epfd,events,20,500);
        //Process all events that occur

    sleep(2);
    
        //cout << "11" << endl;
        for(i=0;i<nfds;++i)
        {        if(connfd>0)         {                 //write(connfd, "i am coming" , 20); }             cout << "22" << endl;             if(events[i].data.fd==listenfd)//If a new SOCKET user is connected to the bound SOCKET port, a new connection is established .             {             cout << "33" << endl;                 connfd = accept(listenfd,(sockaddr *)&clientaddr, &clilen);                 if(connfd<0){                     perror("connfd<0");                     exit(1);
    














EPOLLIN)//If it is a connected user and receives data, then read in.             {














                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';
                cout << "read=" << line << endl;
                //write(connfd, "i am coming", 20);
                //Set the file descriptor for write operation
                ev.data.fd=sockfd;
                // Set write operation event
                ev.events |= EPOLLOUT|EPOLLET;
                //ev.events |= EPOLLIN|EPOLLET;
                //Modify the event to be processed on sockfd to EPOLLOUT
                epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev) ;
            }
            else if(events[i].events&EPOLLOUT) // If there is data to be sent
            {                 cout << "EPOLLOUT" <<",line="<<line<< endl;                 sockfd = events[i].data.fd;                 write(sockfd, line, n);



                //Set the file descriptor used for the read operation                 //
                ev.data.fd=sockfd;
//Set the read operation event used for the annotation measurement                 //
                ev.events=EPOLLIN|EPOLLET;
//Modify the sockfd to be processed The event is EPOLIN
                //epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
            }
        }
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/yinhua405/article/details/112857690