栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言实现poll

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言实现poll

C语言实现poll
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))

int main() {

    int fd = socket(AF_INET, SOCK_STREAM, 0);

    int reuse_port = 1;
    setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &reuse_port, sizeof(int));

    //绑定地址
    struct sockaddr_in bind_address;
    memset(&bind_address, 0, sizeof(struct sockaddr_in));
    bind_address.sin_addr.s_addr = inet_addr("0.0.0.0");
    bind_address.sin_family = AF_INET;
    bind_address.sin_port = htons(8088);
    int code = bind(fd, (struct sockaddr *) &bind_address, sizeof(struct sockaddr_in));
    if (code == -1) {
        perror("bind error");
        close(fd);
        return -1;
    }
    //设置监听状态
    code = listen(fd, 1024);
    if (code == -1) {
        perror("listen error");
        close(fd);
        return -1;
    }

    struct pollfd fds_array[1024];
    for (int i = 0; i < 1024; ++i) {
        fds_array[i].fd = -1;
    }

    fds_array[0].fd = fd;
    fds_array[0].events = POLLIN;
    int max_index = 0;
    while (1) {
        int receive_num = poll(fds_array, max_index + 1, -1);
        if (receive_num == -1) {
            perror("poll error");
            break;
        } else if (receive_num == 0) {
            continue;
        } else {
            if (fds_array[0].revents & POLLIN) {
                printf("acceptn");
                int cfd = accept(fd, NULL, NULL);
                if (cfd == -1) {
                    perror("accept error");
                    continue;
                } else {
                    int find_index = -1;
                    for (int i = 0; i < ARRAY_SIZE(fds_array); ++i) {
                        if (fds_array[i].fd == -1) {
                            fds_array[i].fd = cfd;
                            fds_array[i].events = POLLIN;
                            find_index = i;
                            break;
                        }
                    }
                    if (find_index == -1) {
                        printf("client connect too many,refuse!");
                        continue;
                    }
                    if (find_index > max_index) {
                        max_index = find_index;
                    }
                }
            } else {
                //遍历读取每个连接数据
                for (int i = 1; i <= max_index; ++i) {
                    if (fds_array[i].fd == -1) {
                        continue;
                    }
                    if (fds_array[i].revents & POLLIN) {
                        char buf[1024];
                        size_t read_num = read(fds_array[i].fd, buf, 1024);
                        if (read_num < 0) {
                            perror("read error");
                            close(fds_array[i].fd);
                            fds_array[i].fd = -1;
                            continue;
                        } else if (read_num == 0) {
                            printf("closen");
                            close(fds_array[i].fd);
                            fds_array[i].fd = -1;
                            continue;
                        } else {
                            write(fds_array[i].fd, buf, read_num);
                        }
                    }
                }
            }
        }
    }
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/629640.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号