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;
}
相关推荐
消失的旧时光-194313 分钟前
Linux 入门核心命令清单(工程版)
linux·运维·服务器
艾莉丝努力练剑21 分钟前
【Linux:文件】Ext系列文件系统(初阶)
大数据·linux·运维·服务器·c++·人工智能·算法
Trouvaille ~1 小时前
【Linux】TCP Socket编程实战(一):API详解与单连接Echo Server
linux·运维·服务器·网络·c++·tcp/ip·socket
旖旎夜光1 小时前
Linux(13)(中)
linux·网络
威迪斯特2 小时前
CentOS图形化操作界面:理论解析与实践指南
linux·运维·centos·组件·图形化·桌面·xserver
一方热衷.2 小时前
在线安装对应版本NVIDIA驱动
linux·运维·服务器
独自归家的兔2 小时前
ubuntu系统安装dbswitch教程 - 备份本地数据到远程服务器
linux·运维·ubuntu
m0_694845572 小时前
tinylisp 是什么?超轻量 Lisp 解释器编译与运行教程
服务器·开发语言·云计算·github·lisp
ONE_SIX_MIX2 小时前
ubuntu 24.04 用rdp连接,桌面黑屏问题,解决
linux·运维·ubuntu
龙飞052 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl