epool练习

本文只是在学习过程中写的一个简单事例,只为练习epool的使用,无任何作用,贴文只是在后续复习的时候看到可以回忆起一些信息。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MAX_EPOLL_EVENTS 200
#define BUFF_MAX 100

int init_sock(char *ip, short port)
{
    int iSockfd = 0;
    int iRet = 0;
    struct sockaddr_in server;

    memset(&server, 0, sizeof(server));
    iSockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (0 > iSockfd)
    {
        printf("create socket failed! errno = %d\n error = %s\n", errno, strerror(errno));
        return -1;
    }

    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = inet_addr(ip);
    iRet = bind(iSockfd, (struct sockaddr *)&server, sizeof(struct sockaddr));
    if (0 > iRet)
    {
        printf("bind server failed! errno = %d\n error = %s\n", errno, strerror(errno));
        close(iSockfd);
        return -1;
    }

    iRet = listen(iSockfd, 10);
    if (0 > iRet)
    {
        printf("listen client failed! errno = %d\n error = %s\n", errno, strerror(errno));
        close(iSockfd);
        return -1;
    }
    return iSockfd;
}

int setnonblocking(int iSockfd)
{
    fcntl(iSockfd, F_SETFL, fcntl(iSockfd, F_GETFD, 0)| O_NONBLOCK);
    return 0;
}

int epoll_ctlfunc(int iEpollfd, int iSockfd, int iEtflag)
{
    int iRet = 0;
    struct epoll_event ep_event;

    ep_event.data.fd = iSockfd;
    ep_event.events = EPOLLIN;

    if (iEtflag)
        ep_event.events = EPOLLIN | EPOLLET;

    iRet = epoll_ctl(iEpollfd, EPOLL_CTL_ADD, iSockfd, &ep_event);
    if (0 != iRet)
    {
        printf("add sockfd to epoll failed!\n");
        return -1;
    }
    iRet = setnonblocking(iSockfd);
    if (0 != iRet)
    {
        printf("set sockfd failed !\n");
        return -1;
    }

    return 0;
}

int epoll_process(int iEpollfd, int iSockfd, int num, struct epoll_event *events)
{
    int i = 0;
    struct sockaddr_in client;
    int iLen = sizeof(struct sockaddr_in);
    int iRet = 0;
    int confd = 0;
    int n = 0;
    char buff[BUFF_MAX] = {0};

    //printf("num = %d\n",num);
    for (i = 0; i < num; i++)
    {
        if (iSockfd == (events[i].data.fd))
        {
            confd = accept(iSockfd, (struct sockaddr *)&client, &iLen);
            if (0 > confd)
            {
                printf("accept failed !\n");
                return -1;
            }
            printf("welcome %s : %d !\n", inet_ntoa(client.sin_addr), htons(client.sin_port));

            iRet = epoll_ctlfunc(iEpollfd, confd, 0);
            if (0 > iRet)
            {
                printf("add client socket failed !\n");
            }
        }
        else if (events[i].events & EPOLLIN)
        {
            memset(buff, 0, sizeof(buff));
            n = read(events[i].data.fd, buff, BUFF_MAX);
            if (n > 0)
                printf("recv client[%d] info is %s\n",events[i].data.fd, buff);
            else
            {
                if (n == 0)
                    printf("client close!\n");
                close(events[i].data.fd);
            }
            write(events[i].data.fd, buff, BUFF_MAX);
        }
    }
    return 0;
}
int main(int argc, char **argv)
{
    int iSockfd = 0;
    int iEpollfd = 0;
    int iRet = 0;
    struct epoll_event events[MAX_EPOLL_EVENTS];

    if (3 != argc)
    {
        printf("./xx ip server!\n");
        exit(0);
    }

    iSockfd = init_sock(argv[1], atoi(argv[2]));
    if (-1 == iSockfd)
    {
        printf("init socket failed!\n");
        exit(0);
    }

    iEpollfd = epoll_create(MAX_EPOLL_EVENTS);
    if (-1 == iEpollfd)
    {
        printf("create epoll failed !\n");
        exit(0);
    }

    iRet = epoll_ctlfunc(iEpollfd, iSockfd, 1);
    if (0 != iRet)
    {
        printf("epoll_ctl function failed !\n");
        exit(0);
    }

    while (1)
    {
        iRet = epoll_wait(iEpollfd, events, MAX_EPOLL_EVENTS, -1);
        //iRet = epoll_wait(iEpollfd, events, MAX_EPOLL_EVENTS, 0);
        if (0 > iRet)
        {
            printf("epoll_wait failed !\n");
            exit(0);
        }
        //printf("iRet = %d\n",iRet);
        iRet = epoll_process(iEpollfd, iSockfd, iRet, events);
        if (0 > iRet)
        {
            printf("process data failed !\n");
            exit(0);
        }

    }
    close(iSockfd);
    return 0;
}

你可能感兴趣的:(C语言)