ubuntu tcp epoll

h文件 

#ifndef NETWORKTCPEPOLL_H
#define NETWORKTCPEPOLL_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

class NetworkTcpServiceEpoll
{
public:
    NetworkTcpServiceEpoll();

    int networkTCP_Service_Open(int port);

    int networkTCP_Service_Write(uint8_t *send_data, size_t data_len);

    int networkTCP_Service_Read(uint8_t *recv_data,  size_t data_len);

    void networkTCP_Service_Close();

private:
    struct epoll_event ev,events[20];
    int listenfd,sockfd,epfd;

    int  network_TCP_Service_Thread();
};

#endif // NETWORKTCPEPOLL_H

cpp文件:

#include "networktcpepoll.h"

NetworkTcpServiceEpoll::NetworkTcpServiceEpoll()
{
    listenfd = 0;
    sockfd = 0;
    epfd = 0;
}

int NetworkTcpServiceEpoll::networkTCP_Service_Open(int port)
{
    listenfd = socket(AF_INET,SOCK_STREAM,0);

    //设置非阻塞
    int opts = fcntl(listenfd, F_GETFL);
    if(opts < 0)
    {
        perror("fcntl(sock,GETFL)");
        exit(1);
    }
    opts = opts | O_NONBLOCK;
    if(fcntl(listenfd, F_SETFL, opts) < 0)
    {
        perror("fcntl(sock,SETFL,opts)");
        exit(1);
    }

    epfd = epoll_create(256);
    ev.data.fd = listenfd;
    ev.events = EPOLLIN | EPOLLET;
    epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev);

    //服务器地址绑定
    struct sockaddr_in serveraddr;
    bzero(&serveraddr,sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = INADDR_ANY;
    serveraddr.sin_port = htons(port);
    bind(listenfd, (const sockaddr*)&serveraddr, sizeof(serveraddr));
    listen(listenfd, 20);

    boost::thread(&NetworkTcpServiceEpoll::network_TCP_Service_Thread, this);

    return listenfd;
}

int NetworkTcpServiceEpoll::networkTCP_Service_Write(uint8_t *send_data, size_t data_len)
{
    int len = write(sockfd, send_data, data_len);

    return len;
}

int NetworkTcpServiceEpoll::networkTCP_Service_Read(uint8_t *recv_data, size_t data_len)
{
    int len = read(sockfd, recv_data, data_len);

    return len;
}

void NetworkTcpServiceEpoll::networkTCP_Service_Close()
{
    close(listenfd);
}

int NetworkTcpServiceEpoll::network_TCP_Service_Thread()
{
    struct sockaddr_in clientaddr;
    socklen_t client = sizeof(clientaddr);

    while(true) {
        int nfds = epoll_wait(epfd,events,20,500);
        for(int i = 0;i < nfds;i ++) {
            if(events[i].data.fd == listenfd) {//如果新监测到一个SOCKET用户连接到了绑定的SOCKET端口,建立新的连接。
                int connfd = accept(listenfd,(sockaddr*)&clientaddr,&client);
                if(connfd < 0){
                    perror("accept<0");
                    return -1;
                }
                //设置非阻塞
                int opts = fcntl(listenfd, F_GETFL);
                if(opts < 0)
                {
                    perror("fcntl(sock,GETFL)");
                    exit(1);
                }
                opts = opts | O_NONBLOCK;
                if(fcntl(listenfd, F_SETFL, opts) < 0)
                {
                    perror("fcntl(sock,SETFL,opts)");
                    exit(1);
                }

                printf("ip client:%s\n",inet_ntoa(clientaddr.sin_addr));
                ev.data.fd = connfd;
                ev.events = EPOLLIN | EPOLLET;
                epoll_ctl(epfd,EPOLL_CTL_ADD,connfd,&ev);
            }else if (events[i].events & EPOLLIN) {//如果是已经连接的用户,并且收到数据,那么进行读入。
                uint8_t buf[1024*1000] = {0};
                if ((sockfd = events[i].data.fd) < 0) continue;//确定连接用户存在
                int buf_len = networkTCP_Service_Read(buf, sizeof(buf) - 1);
                buf[buf_len] = '\0';
                if (buf_len< 0){
                    if(errno == ECONNRESET) {
                        close(sockfd);
                        events[i].data.fd = -1;
                    } else
                        cout<<"readline error"<

main文件

#include 
#include "networktcpepoll.h"

using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    NetworkTcpServiceEpoll network;

    int ret = network.networkTCP_Service_Open(8088);

    cout<<"ret : "<

你可能感兴趣的:(C/C++,ubuntu,tcp/ip,linux)