Loading

【Linux网络编程】基于 EPOLL 的 SOCKET 通信

【Linux网络编程】基于 EPOLL 的 SOCKET 通信

epoller.h

#ifndef EPOLLER_H
#define EPOLLER_H

#include <sys/epoll.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <vector>

class Epoller {
public:
    explicit Epoller(int maxEvent = 1024);
    ~Epoller();
    bool AddFd(int fd, uint32_t events);
    bool ModFd(int fd, uint32_t events);
    bool DelFd(int fd);
    int Wait(int timeoutMs = -1);
    int GetEventFd(size_t i) const;
    uint32_t GetEvents(size_t i) const;

private:
    int epollFd_;  // epoll_create()创建一个epoll对象,返回值为epollFd
    std::vector<struct epoll_event> events_;  // 检测到的事件的集合
};

#endif  // EPOLLER_H

epoller.cpp

#include "epoller.h"

// 创建epoll对象 epoll_create(512)
Epoller::Epoller(int maxEvents) : epollFd_(epoll_create(512)), events_(maxEvents) {
    assert(epollFd_ >= 0 && events_.size() > 0);
}

Epoller::~Epoller() {
    close(epollFd_);
}

bool Epoller::AddFd(int fd, uint32_t events) {
    if (fd < 0) return false;
    epoll_event ev = {0};
    ev.data.fd = fd;
    ev.events = events;
    return 0 == epoll_ctl(epollFd_, EPOLL_CTL_ADD, fd, &ev);
}

bool Epoller::ModFd(int fd, uint32_t events) {
    if (fd < 0) return false;
    epoll_event ev = {0};
    ev.data.fd = fd;
    ev.events = events;
    return 0 == epoll_ctl(epollFd_, EPOLL_CTL_MOD, fd, &ev);
}

bool Epoller::DelFd(int fd) {
    if (fd < 0) return 0;
    epoll_event ev = {0};
    return 0 == epoll_ctl(epollFd_, EPOLL_CTL_DEL, fd, &ev);
}

int Epoller::Wait(int timeoutMs) {
    return epoll_wait(epollFd_, &events_[0], static_cast<int>(events_.size()), timeoutMs);
}

int Epoller::GetEventFd(size_t i) const {
    assert(i < events_.size() && i >= 0);
    return events_[i].data.fd;
}

uint32_t Epoller::GetEvents(size_t i) const {
    assert(i < events_.size() && i >= 0);
    return events_[i].events;
}

webserver.h

#ifndef WEBSERVER_H
#define WEBSERVER_H

#include <stdio.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <memory>
#include "epoller.h"

class WebServer {
public:
    WebServer(int port, int timeoutMs);
    ~WebServer() {}
    // void Start();
private:
    bool InitSocket_();
    int timeoutMs_;
    int port_;
    int listenFd_;
    std::unique_ptr<Epoller> epoller_;
};

#endif  // WEBSERVER_H

webserver.cpp

#include "webserver.h"

WebServer::WebServer(int port, int timeoutMs) : port_(port), timeoutMs_(timeoutMs),
                    epoller_(new Epoller()) {
    InitSocket_();
}

bool WebServer::InitSocket_() {
    // 创建socket
    listenFd_= socket(AF_INET, SOCK_STREAM, 0);
    if (listenFd_ == -1) {
        perror("socket");
        return -1;
    }

    // 设置端口复用
    int optval = 1;
    setsockopt(listenFd_, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));

    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = INADDR_ANY;
    saddr.sin_port = htons(port_);
    // 绑定
    int ret = bind(listenFd_, (struct sockaddr*)&saddr, sizeof(saddr));
    if (ret == -1) {
        perror("bind");
        return -1;
    }

    // 监听
    ret == listen(listenFd_, 128);
    if (ret == -1) {
        perror("listen");
        return -1;
    }

    // 将listenFd_注册至epoll事件表中
    epoller_->AddFd(listenFd_, EPOLLIN);

    while (1) {
        int num = epoller_->Wait(timeoutMs_);
        if (num == -1) {
            perror("epoll_wait");
            exit(-1);
        }
        printf("num = %d\n", num);
        for (int i = 0; i < num; ++i) {
            int curfd = epoller_->GetEventFd(i);
            if (curfd == listenFd_) {  // 有客户端连接进来
                struct sockaddr_in cliaddr;
                socklen_t len = sizeof(cliaddr);
                int cfd = accept(listenFd_, (struct sockaddr*)&cliaddr, &len);

                // 获取客户端信息
                char cliIp[16];
                inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, cliIp, sizeof(cliIp));
                unsigned short cliPort = ntohs(cliaddr.sin_port);
                // 输出客户端的信息
                printf("client's ip is %s, and port is %d\n", cliIp, cliPort );

                int flag = fcntl(cfd, F_GETFL);
                flag |= O_NONBLOCK;  // 设置文件描述符非阻塞
                fcntl(cfd, F_SETFL, flag);

                epoller_->AddFd(cfd, EPOLLIN | EPOLLET);  // cfd注册至内核事件表中,并设置ET模式
            } else {  // 有客户端发送数据
                if (epoller_->GetEvents(i) & EPOLLOUT) {
                    continue;
                }
                char buf[5];
                int len = 0;
                while ((len = read(curfd, buf, sizeof(buf))) > 0) {
                    write(STDOUT_FILENO, buf, len);
                    write(curfd, buf, len);
                }
                if (len == 0) {
                    printf("client close...\n");
                } else if (len == -1) {
                    if (errno == EAGAIN) {
                        printf("read over ...\n");
                    } else {
                        perror("read\n");
                        exit(-1);
                    }
                }
            }
        }
    }
    close(listenFd_);
    return 0;
}

main.cpp

#include "../server/webserver.h"

int main() {
    WebServer webserver(1316, -1);  // 设置端口, 超时时间
    // webserver.Start();
    return 0;
}

client.cpp

#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main() {
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd == -1) {
        perror("socket");
        return -1;
    }

    struct sockaddr_in seraddr;
    inet_pton(AF_INET, "127.0.0.1", &seraddr.sin_addr.s_addr);
    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(1316);
    // 连接服务器
    int ret = connect(fd, (struct sockaddr*)&seraddr, sizeof(seraddr));
    if (ret == -1) {
        perror("connect");
        return -1;
    }

    char sendBuf[1024] = {0};
    while (1) {
        fgets(sendBuf, sizeof(sendBuf), stdin);
        write(fd, sendBuf, strlen(sendBuf) + 1);
        int len = read(fd, sendBuf, sizeof(sendBuf));
        if (len == -1) {
            perror("read");
            return -1;
        } else if (len > 0) {
            printf("read buf = %s\n", sendBuf);
        } else {
            printf("server disconnect...\n");
            break;
        }
    }

    close(fd);
    return 0;
}
posted @ 2024-08-28 20:09  杨谖之  阅读(27)  评论(0编辑  收藏  举报