【C/C++】实现Reactor高并发服务器 三 增加 InetAddress 类

-rw-rw-r-- 1 king king  1296 Feb  1 11:56 client.cpp
-rwxrwxr-x 1 king king 24344 Feb  1 11:56 client
-rw-rw-r-- 1 king king  4437 Feb  1 13:32 tcpepoll.cpp
-rw-rw-r-- 1 king king   204 Feb  1 13:33 makefile
-rw-rw-r-- 1 king king   460 Feb  1 13:34 InetAddress.h
-rwxrwxr-x 1 king king 95088 Feb  1 13:36 tcpepoll
-rw-rw-r-- 1 king king   618 Feb  1 13:36 InetAddress.cpp

#ifndef _INETADDRESS_H
#define _INETADDRESS_H

#pragma on_INETADDRESS_He

#include <string>
#include <arpa/inet.h>
#include <netinet/in.h>


class InetAddress
{
private:
    sockaddr_in addr_;
public:
    InetAddress(const std::string &ip, uint16_t port);
    InetAddress(const sockaddr_in addr);
    ~InetAddress();
    const char *ip()const;
    uint16_t port()const;
    const sockaddr *addr()const;
};

#endif // _INETADDRESS_H



#include "InetAddress.h"




InetAddress::InetAddress(const std::string &ip, uint16_t port)
{
    struct sockaddr_in servaddr;
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(ip.c_str());
    servaddr.sin_port = htons(port);
}

InetAddress::InetAddress(const sockaddr_in addr):addr_(addr)
{
    
}

InetAddress::~InetAddress()
{

}

const char* InetAddress::ip()const
{
    return inet_ntoa(addr_.sin_addr);
}
uint16_t InetAddress::port()const
{
    return ntohs(addr_.sin_port);
}
const sockaddr* InetAddress::addr()const
{
    return (sockaddr*)&addr_;
}


#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 "InetAddress.h"


// 设置非阻塞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]));
    */
    InetAddress servaddr(std::string(argv[1]), atoi(argv[2]));

    if(bind(listenfd, servaddr.addr(), sizeof(sockaddr)) < 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 peeraddr;
                    socklen_t len = sizeof(peeraddr);
                    InetAddress clientaddr(peeraddr);
                    //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, clientaddr.ip(), clientaddr.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;
} 


all: client tcpepoll

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

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

 
clean:
	rm -f client tcpepoll

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