select实现echo服务器的并发

select实现echo服务器的并发

代码实现

c 复制代码
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>


#define MAX_CLI 50

int clients[MAX_CLI];
struct sockaddr_in clientaddr;
socklen_t len = sizeof(clientaddr);

int socket_create(int port) {//创建并绑定网络监听的socket套接字
    int fd;
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        exit(EXIT_FAILURE);
    }
    struct sockaddr_in addr;
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(port);
    if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        perror("bind");
        exit(EXIT_FAILURE);
    }
    if (listen(fd, 20) < 0) {
        perror("listen");
        exit(EXIT_FAILURE);
    }
    return fd;
}
int recv_msg(int fd, char *buff, int size) { 

    int rsize = recv(fd, buff, sizeof(char ) * size, 0);
    if (rsize <= 0) {
        return -1;
    }
    printf("Recv : %s\n", buff);
    return 0;
}

int send_msg(int fd, char *buff, int size) {
    int ssize = send(fd, buff, sizeof(char ) * size, 0);
    if (ssize <= 0) {
        return -1;
    }
    printf("Send success!\n");
    return 0;
}

void echo_work(int server_listen) {
    for (int i = 0; i < MAX_CLI; i++) clients[i] = -1;//初始化数组
    while (1) {
        fd_set rfds;
        FD_ZERO(&rfds);
        FD_SET(server_listen, &rfds);
        int max_fd = server_listen;

        for (int i = 0; i < MAX_CLI; i++) {
            int fd = clients[i];
            if (fd != -1) {
                FD_SET(fd, &rfds);
                max_fd = fd > max_fd ? fd : max_fd;
            }
        }

        int ret = select(max_fd + 1, &rfds, NULL, NULL, NULL);
        if (ret < 0) {
            perror("select");
            exit(EXIT_FAILURE);
        }
        if (FD_ISSET(server_listen, &rfds)) {
            int clientfd;
            if((clientfd = accept(server_listen, (struct sockaddr *)&clientaddr, &len)) < 0) {
                perror("accept");
                exit(EXIT_FAILURE);
            }
            printf("one client accpet succes\n");
            for (int i = 0; i < MAX_CLI; i++) {
                if (clients[i] == -1) {
                    clients[i] = clientfd;
                    break;
                }
            } 
        }

        for (int i = 0; i < MAX_CLI; i++) {
            int fd = clients[i];
            if (fd != -1 && FD_ISSET(fd, &rfds)) {
                char buff[1024] = {0};
                if (recv_msg(fd, buff, 1024) < 0) {
                    clients[i] = -1;
                    close(fd);
                }//接收数据

                if (send_msg(fd, buff, 1024) < 0) {
                    clients[i] = -1;
                    close(fd);
                }//回显数据
            }
        }
    }
    
    return ;
}


int main() {
    int server_listen;

    if ((server_listen = socket_create(8080)) < 0) {
        perror("socket_create");
        exit(EXIT_FAILURE);
    }
    
    echo_work(server_listen);

    return 0;
}
相关推荐
donoot13 分钟前
Linux系统下图书馆级电子书全自动标准化分类整理完整实施方案
大数据·linux·运维·电子书管理
Felix-lxd1 小时前
Ubuntu 22.04 配置 Nginx
linux·nginx·ubuntu
大E帝国子民12 小时前
MacOS 安装Seismic Unix
服务器·macos·unix
Tim_Van2 小时前
在 CentOS 7 中安装 Chromium 的完整步骤
linux·运维·chrome·centos
德福危险2 小时前
富有想象力的XSS盲打:靶机练习之Tempus_fugit5
服务器·网络·xss
2023自学中2 小时前
C++ 内存追踪器
linux·c++
啵啵鱼爱吃小猫咪4 小时前
Ubuntu 20.04 + ROS Noetic 源码安装 libfranka 0.8.0 和 franka_ros 0.8.0 完整教程
linux·运维·ubuntu
ljs6482739515 小时前
Linux运维实操:vi编辑器永久配置静态IP(CentOS系列)
linux·运维·编辑器
dddwjzx5 小时前
嵌入式Linux C应用编程入门——高级I/O
linux·嵌入式
Waay6 小时前
Linux 三个核心环境变量配置文件、作用域、生效方式完整梳理
linux·运维·学习·云原生·容器