epoll服务器

#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 100


static 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值