epoll服务器

来源:互联网 发布:英语文献翻译软件 编辑:程序博客网 时间:2024/05/18 02:57
#include <sys/epoll.h>#include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#define MAX_EPOLL_SIZE 100static void usage(const char* pro){    printf("%s [local_ip][local_port]\n", pro);}int startup(const char* _ip, int _port){    //  chuangjian  taojiezi    int sock = socket(AF_INET, SOCK_STREAM, 0);    if(sock < 0){        perror("socket");        exit(3);    }    //  bangding    struct sockaddr_in local;    local.sin_family = AF_INET;    local.sin_port = htons(_port);    local.sin_addr.s_addr = inet_addr(_ip);    if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0){        perror("bind");        exit(4);    }    //  jianting    if(listen(sock, 10) < 0){        perror("listen");    }    return sock;}int main(int argc, char* argv[]){    if(argc != 3)    {        usage(argv[0]);        exit(1);    }    int listen_sock = startup(argv[1], atoi(argv[2]));    int ret = epoll_create(200);    if(ret < 0)    {        perror("epoll_create");        exit(5);    }    struct epoll_event ev;    ev.data.fd = listen_sock;    ev.events = EPOLLIN;    struct epoll_event ev_array[MAX_EPOLL_SIZE];    int i = 0;    if(epoll_ctl(ret, EPOLL_CTL_ADD, listen_sock, &ev) < 0)    {        perror("epoll_ctl");        exit(6);    }    while(1)    {        int key = epoll_wait(ret, ev_array, MAX_EPOLL_SIZE, 1000);        switch(key)        {            case 0:                {                    printf("no discriptor is ready\n");                    break;                }            case -1:                {                    perror("epoll_wait");                    break;                }            default:                {                    for(i = 0;i < key; ++i)                    {                        if((ev_array[i].data.fd == listen_sock) && (ev_array[i].events & EPOLLIN))  //listen_sock is ready                        {                            struct sockaddr_in client;                            socklen_t len = sizeof(client);                            int new_sock = accept(ev_array[i].data.fd, (struct sockaddr*)&client, &len);                            if(new_sock < 0)                            {                                perror("accept");                                continue;                            }                            //add new_sock to red_black_tree                            printf("get a new client : %s %d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));                            struct epoll_event new_ev;                            new_ev.data.fd = new_sock;                            new_ev.events = EPOLLIN; //| EPOLLOUT;                            if(epoll_ctl(ret, EPOLL_CTL_ADD, new_sock, &new_ev) < 0)                            {                                close(new_sock);                                perror("epoll_ctl");                            }                                continue;                        }                        else if(ev_array[i].events & EPOLLIN) // other sock read_ting is ready                        {                            char buf[1024];                            ssize_t s = read(ev_array[i].data.fd, buf, sizeof(buf)-1);                            if(s > 0)                            {                                buf[s] = 0;                                printf("client -> server :%s\n", buf);                            }                            else if(s == 0)                            {                                printf("client qute...\n");                                epoll_ctl(ret, EPOLL_CTL_DEL, ev_array[i].data.fd, &ev_array[i]);                                close(ev_array[i].data.fd);                                continue;                            }else                            {                                perror("read");                                continue;                            }                        }                        else if(ev_array[i].events & EPOLLOUT)   // other sock write_ting is ready                        {                            char* pStr = "liaoyueling is a pig";                            write(ev_array[i].data.fd, pStr, strlen(pStr));                            epoll_ctl(ret, EPOLL_CTL_DEL, ev_array[i].data.fd, &ev_array[i]);                            close(ev_array[i].data.fd);                            continue;                        }                    }//for                }//deault        }//switch    }//while    return 0;}
原创粉丝点击