【C/C++】实现Reactor高并发服务器 二 优化一下服务器代码

tcpepoll.cpp



#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/fcntl.h>
#include <sys/epoll.h>
#include <netinet/tcp.h>  // TCP_NODELAY




// 设置非阻塞IO
void setnonblocking(int fd)
{
    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
}

int main(int argc, char *argv[])
{
    if(argc !=3)
    {
        printf("usage: ./tcpepoll ip port");
        return -1;
    }


    int listenfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
    if(listenfd < 0)
    {
        perror("socket() failed"); return -1;
    }  

    int opt = 1;
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, static_cast<socklen_t>(sizeof(opt)));
    setsockopt(listenfd, SOL_SOCKET, TCP_NODELAY, &opt, static_cast<socklen_t>(sizeof(opt)));
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEPORT, &opt, static_cast<socklen_t>(sizeof(opt)));
    setsockopt(listenfd, SOL_SOCKET, SO_KEEPALIVE, &opt, static_cast<socklen_t>(sizeof(opt)));

    //setnonblocking(listenfd);

    struct sockaddr_in servaddr;
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);
    servaddr.sin_port = htons(atoi(argv[2]));

    if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("bind() failed");
        close(listenfd);
        return -1;
    }

    if(listen(listenfd, 128) != 0)
    {
        close(listenfd);
        return -1;
    }

    int epollfd = epoll_create(1);

    struct epoll_event ev;
    ev.data.fd = listenfd;
    ev.events=EPOLLIN; //水平

    epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &ev);

    struct epoll_event evs[10];
    while(1)
    {
        int infds = epoll_wait(epollfd, evs, 10, -1);
        if(infds < 0)
        {
            perror("epoll_wait() failed "); break;
        }
        for(int i = 0; i < infds; i++)
        {

            if(evs[i].events & EPOLLRDHUP)
            {
                close(evs[i].data.fd);
            }
            else if (evs[i].events & EPOLLIN|EPOLLPRI)
            {

                        
                if(evs[i].data.fd == listenfd)
                {
                    struct sockaddr_in clientaddr;
                    socklen_t len = sizeof(clientaddr);
                    //accept --> accept4
                    int clientfd = accept4(listenfd, (struct sockaddr*)&clientaddr, &len, SOCK_NONBLOCK);
                    //setnonblocking(clientfd);
                    printf("accept client fd=%d, ip=%s,port=%d ok.\n", clientfd, inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));

                    ev.data.fd = clientfd;
                    ev.events=EPOLLIN|EPOLLET; //边缘
                    epoll_ctl(epollfd, EPOLL_CTL_ADD, clientfd, &ev);

                }
                else{

                    char buffer[1024];
                    while(true){
                        memset(buffer, 0, sizeof(buffer));
                        size_t nread = recv(evs[i].data.fd, buffer, sizeof(buffer), 0);
                        if(nread > 0)
                        {
                            printf("tcpepoll Recv:%s\n", buffer);
                            send(evs[i].data.fd, buffer, strlen(buffer), 0);
                        }else if (nread == -1 && errno == EINTR)
                        {
                            continue;
                        }else if(nread == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))
                        {
                            break;
                        }else if (nread == 0)
                        {
                            printf("clientfd:%d disconnected\n", evs[i].data.fd);
                            close(evs[i].data.fd);
                            break;
                        }
                    }
                }
            }
            else if (evs[i].events & EPOLLOUT)
            {
            }
            else
            {
                        close(evs[i].data.fd);
            }  
            
        }
    }
    close(listenfd);
    return 0;
}

你可能感兴趣的:(服务器,C++学习,c++)