#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/resource.h>


#define MAXBUF 1024
#define MAXEPOLLSIZE 10000
int kdpfd ;

int  send_string(int)  ;

/*
setnonblocking
*/
int setnonblocking(int sockfd)
{
    if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1) {
        return -1;
    }
    return 0;
}

/*
handle_message - handle every socket recv
*/
int handle_message(int new_fd)
{
    char buf[MAXBUF + 1];
    int len;
    bzero(buf, MAXBUF + 1);
    int clientdone = 0 ;
    struct epoll_event event ;

    while(1)
    {
        len = recv(new_fd, buf, MAXBUF, 0);
        if(len < 0)
        {
            if(errno!= EAGAIN)
            {
                printf("recv error...\n") ;
                clientdone = 1 ;
            }
            break ;
        }else if(len==0)
        {
            clientdone = 1 ; //remote close connection
            printf("remote close connection...\n") ;
            break ;
        }else{

            printf("%d receive '%s' len=%d\n", new_fd, buf, len);
            if(strncmp(buf,"zzzzz",5) != 0)
            {
                event.data.fd = new_fd ;
                event.events = EPOLLOUT | EPOLLET;
                int s ;
                s = epoll_ctl(kdpfd , EPOLL_CTL_MOD, new_fd, &event);
                if (s == -1) {
                    perror("epoll_ctl");
                    abort();
                }
                // .. header
                clientdone = send_string(new_fd);
                printf("send hello world \n") ;
            }
        }
    }//while
    return clientdone  ;

    /*
    len = recv(new_fd, buf, MAXBUF, 0);
    if (len > 0)
        printf
            ("%d receive '%s' len=%d\n",
             new_fd, buf, len);
    else {
        if (len < 0)
        {
            printf ("receive error,error=%d,'%s'\n", errno, strerror(errno));
        }
        close(new_fd);
        return -1;
    }
    return len;
    */

} //handle_message

int  send_string(int new_fd)
{
    char strx[16] ;
    memset(strx,0x00,sizeof(strx)) ;
    strcpy(strx,"hello world\n") ;
    send(new_fd, strx, strlen(strx), 0) ;
    /*
    while(1)
    {
        if( send(new_fd, strx, strlen(strx), 0) < 0 )
        {
            if (errno != EAGAIN)
            {
                perror("send in send_string error");
                return 1 ;
            }else
                return 0 ;
        }
    }//while
    */
    return 0 ;
}

/*********** about this file *************
*filename: epoll-server.c
*wrote by: zhoulifa(zhoulifa@163.com)(http://zhoulifa.bokee.com)
*date time:2007-01-31 21:00
*Thanks to:Google
*********************************************************************/

int main(int argc, char **argv)
{
    int listener, new_fd, nfds, n, ret, curfds;
    socklen_t len;
    struct sockaddr_in my_addr, their_addr;
    unsigned int myport, lisnum;
    struct epoll_event ev;
    struct epoll_event events[MAXEPOLLSIZE];
    struct rlimit rt;

    if (argv[1])
        myport = atoi(argv[1]);
    else
        myport = 7838;

    if (argv[2])
        lisnum = atoi(argv[2]);
    else
        lisnum = 2;

    /* config max fd in process */
    rt.rlim_max = rt.rlim_cur = MAXEPOLLSIZE;
    if (setrlimit(RLIMIT_NOFILE, &rt) == -1) {
        perror("setrlimit");
        exit(1);
    }
    else printf("resource config  successfully \n");

    if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    } else

        printf("socket create successfully \n");

    setnonblocking(listener);

    bzero(&my_addr, sizeof(my_addr));
    my_addr.sin_family = PF_INET;
    my_addr.sin_port = htons(myport);
    if (argv[3])
        my_addr.sin_addr.s_addr = inet_addr(argv[3]);
    else
        my_addr.sin_addr.s_addr = INADDR_ANY;


    int opt = 1;
    setsockopt(listener , SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));


    if (bind
        (listener, (struct sockaddr *) &my_addr, sizeof(struct sockaddr))
        == -1) {
        perror("bind");
        exit(1);
    } else
        printf("IP bind successfully \n") ;

    kdpfd = epoll_create(MAXEPOLLSIZE);
    len = sizeof(struct sockaddr_in);
    ev.events = EPOLLIN | EPOLLET;
    ev.data.fd = listener;
    if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listener, &ev) < 0) {
        fprintf(stderr, "epoll set insertion error: fd=%d\n", listener);
        return -1;
    } else
        printf("watch socket to epoll successfully \n");
    curfds = 1;
    while (1)
    {
        /* wait something happen */
        nfds = epoll_wait(kdpfd, events, curfds, -1);
        if (nfds == -1) {
            perror("epoll_wait");
            break;
        }
        /* handle all events */

        for (n = 0; n < nfds; ++n) {
            if (events[n].data.fd == listener) {
                while(1)
                {
                    new_fd = accept(listener, (struct sockaddr *) &their_addr,
                            &len);
                    if (new_fd < 0) {
                        if((errno== EAGAIN)|| (errno== EWOULDBLOCK))
                        {
                            /* We have processed all incoming
                            connections. */
                            break;
                        }
                        else
                        {
                            perror("accept");
                            break;
                        }
                    } else{
                        printf("connect from (%s):(%d),socket:%d\n",
                        inet_ntoa(their_addr.sin_addr),
                        ntohs(their_addr.sin_port), new_fd);
                    }
                    setnonblocking(new_fd);
                    ev.events = EPOLLIN | EPOLLET;
                    ev.data.fd = new_fd;
                    if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, new_fd, &ev) < 0) {
                        fprintf(stderr, "put socket '%d'join epoll fail%s\n",
                            new_fd, strerror(errno));
                        return -1;
                    }
                    curfds++;
                }//while

                continue ;
            } else {
                if(events[n].events & EPOLLIN)
                {
                    ret = handle_message(events[n].data.fd);
                    if(ret==1)
                    {
                        close(events[n].data.fd) ;
                        epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd,&ev);
                        curfds--;
                    }
                }
                if(events[n].events & EPOLLOUT)
                {
                    ev.events = EPOLLIN | EPOLLET;
                    ev.data.fd = events[n].data.fd ;
                    if (epoll_ctl(kdpfd, EPOLL_CTL_MOD, events[n].data.fd, &ev) < 0) {
                        fprintf(stderr, "'%d' mod to EPOLLIN error %s\n",
                            events[n].data.fd, strerror(errno));
                        return -1;
                    }
                    printf("EPOLLOUT changed to EPOLLIN \n") ;
                }
            }
        }

    }
    close(listener);
    return 0;
}

 

創作者介紹
創作者 hedgezzz 的頭像
hedgezzz

hedgezzz的部落格

hedgezzz 發表在 痞客邦 留言(0) 人氣()