ZBLOG

c++代码实现一个tcp高性能网络服务器

下面是一个简单的C++代码实现一个基于epoll的tcp高性能网络服务器:

#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>

#define MAX_EVENTS 1024
#define BUF_SIZE 4096

using namespace std;

int main(int argc, char* argv[])
{
    int listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (listen_fd == -1) {
        cerr << "Failed to create socket" << endl;
        return -1;
    }

    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(12345);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(listen_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
        cerr << "Failed to bind socket" << endl;
        return -1;
    }

    if (listen(listen_fd, 5) == -1) {
        cerr << "Failed to listen" << endl;
        return -1;
    }

    int epoll_fd = epoll_create(MAX_EVENTS);
    if (epoll_fd == -1) {
        cerr << "Failed to create epoll" << endl;
        return -1;
    }

    struct epoll_event ev, events[MAX_EVENTS];
    ev.events = EPOLLIN; // 监听可读事件
    ev.data.fd = listen_fd;
    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &ev) == -1) {
        cerr << "Failed to add listen socket to epoll" << endl;
        return -1;
    }

    while (true) {
        int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
        if (nfds == -1) {
            cerr << "Failed to wait for epoll events" << endl;
            break;
        }

        for (int i = 0; i < nfds; ++i) {
            int fd = events[i].data.fd;

            if (fd == listen_fd) { // 新连接
                struct sockaddr_in client_addr;
                socklen_t len = sizeof(client_addr);
                int client_fd = accept(listen_fd, (sockaddr*)&client_addr, &len);
                if (client_fd == -1) {
                    cerr << "Failed to accept client socket" << endl;
                    continue;
                }
                cout << "New client connected: " << inet_ntoa(client_addr.sin_addr) << ":" << ntohs(client_addr.sin_port) << endl;

                ev.events = EPOLLIN | EPOLLET; // 监听可读事件和边沿触发
                ev.data.fd = client_fd;
                if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev) == -1) {
                    cerr << "Failed to add client socket to epoll" << endl;
                    close(client_fd);
                }
            } else { // 有数据可读
                char buf[BUF_SIZE];
                int nread = read(fd, buf, BUF_SIZE);
                if (nread == -1) {
                    cerr << "Failed to read data from socket" << endl;
                    epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, NULL); // 删除该socket
                    close(fd);
                } else if (nread == 0) {
                    cout << "Client disconnected" << endl;
                    epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, NULL); // 删除该socket
                    close(fd);
                } else {
                    cout << "Received data from client: " << buf << endl;
                    write(fd, buf, nread); // 原样返回
                }
            }
        }
    }

    close(epoll_fd);
    close(listen_fd);

    return 0;
}

这个服务器使用了epoll实现高效的事件驱动,支持边沿触发模式,处理新连接和数据读写。具体实现细节可以查看代码注释。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://sdn.0voice.com/?id=212

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?