【C/C++】实现Reactor高并发服务器 一

一 简单epoll服务端程序 一步一步完善

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, 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].data.fd == listenfd)
            {
                struct sockaddr_in clientaddr;
                socklen_t len = sizeof(clientaddr);
                int clientfd = accept(listenfd, (struct sockaddr*)&clientaddr, &len);
                setnonblocking(clientfd);
                ev.data.fd = clientfd;
                ev.events=EPOLLIN|EPOLLET; //边缘
                epoll_ctl(epollfd, EPOLL_CTL_ADD, clientfd, &ev);
            }else
            {
                    if(evs[i].events & EPOLLRDHUP)
                    {
                        close(evs[i].data.fd);
                    }
                    else if (evs[i].events & EPOLLIN|EPOLLPRI)
                    {
                        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;
}

tcpclient.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
#include <time.h>


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

    int sockfd;
    struct sockaddr_in servaddr;
    char buf[1024];

    if((sockfd=socket(AF_INET,SOCK_STREAM, 0)) < 0)
    {
        return -1;
    }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(atoi(argv[2]));

    if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))!=0)
    {
        return -1;
    }
    for(int i = 0; i < 200000; i++)
    {
        memset(buf, 0, sizeof(buf));
        printf("please input:");
        scanf("%s", buf);
        if(send(sockfd, buf, strlen(buf), 0) < 0)
        {
            close(sockfd);
            return -1;
        }
        memset(buf, 0, sizeof(buf));
        if(recv(sockfd, buf, sizeof(buf), 0) <= 0)
        {
            return -1;
        }
        printf("i:%d recv:%s\n", i, buf);
    }
    return 0;
}

makefile



all: client tcpepoll

client: client.cpp
	g++ client.cpp -g -o client

tcpepoll:tcpepoll.cpp 
	
	g++ tcpepoll.cpp -g -o tcpepoll


clean:
	rm -f client tcpepoll

你可能感兴趣的:(服务器,c语言,c++,服务器)