TCP并发服务器的多进程实现与多线程实现

TCP并发服务器的多进程实现与多线程实现

一、 TCP并发服务器的多进程实现

代码

c 复制代码
#include <my_head.h>

#define SERVER_IP "192.168.125.11" //  服务器IP
#define SERVER_PORT 6666           //  服务器端口

// 子进程处理客户端信息函数
int deal_client_message(int new_sfd, struct sockaddr_in client_info);
// 回调函数回收僵尸进程函数
void callBack_zembie(int sig);

int main(int argc, const char *argv[])
{
    // 用信号的方式回收僵尸进程
    if (signal(SIGCHLD, callBack_zembie) < 0)
    {
        ERR_MSG("signal");
        return -1;
    }

    // 创建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }

    // 待绑定的服务器的信息
    struct sockaddr_in server_info;
    server_info.sin_addr.s_addr = inet_addr(SERVER_IP);
    server_info.sin_port = htons(SERVER_PORT);
    server_info.sin_family = AF_INET;

    // 绑定
    if (bind(sfd, (struct sockaddr *)&server_info, sizeof(server_info)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }

    // 监听
    if (listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }

    // 客户端连接过来的信息
    struct sockaddr_in client_info;
    socklen_t len = sizeof(client_info);

    // 用于接收客户端连接的文件描述符
    int new_sfd;
    // 子进程的 pid
    pid_t cpid = -1;
    while (1)
    {
        // 连接accept
        new_sfd = accept(sfd, (struct sockaddr *)&client_info, &len);
        if (new_sfd < 0)
        {
            ERR_MSG("accept");
            return -1;
        }
        printf("[%s : %d]已连接 new_fd = %d \n", inet_ntoa(client_info.sin_addr),
               ntohs(client_info.sin_port), new_sfd);
        // 创建子进程,用于处理接收的信息
        cpid = fork();

        // 子进程进行处理
        if (0 == cpid)
        {
            // 关闭父进程的服务器文件描述符
            close(sfd);
            // 调用函数进行处理消息
            deal_client_message(new_sfd, client_info);
            // 关闭新创建的用于接收客户端传来信息的文件描述符
            close(new_sfd);
            // 结束进程,当前进程为子进程
            exit(0);
        }
        else if (cpid < 0)
        {
            ERR_MSG("fork");
            return -1;
        }

        // 下边这部分是父进程的内容
        // 在此次循环中关闭新建的文件描述符,因为下次循环又会创建了
        close(new_sfd);
    }

    // 关闭套接字
    close(sfd);
    return 0;
}

// 回调函数回收僵尸进程函数功能实现
void callBack_zembie(int sig)
{
    // 回收僵尸进程资源
    while (waitpid(-1, NULL, WNOHANG) > 0)
        ;
}

// 子进程处理客户端信息函数功能实现
int deal_client_message(int new_sfd, struct sockaddr_in client_info)
{
    char buff[128];
    ssize_t res = -1;

    while (1)
    {
        bzero(buff, sizeof(buff));
        // 接收消息,以阻塞方式接收
        res = recv(new_sfd, buff, sizeof(buff), 0);
        if (res < 0)
        {
            ERR_MSG("recv");
            break;
        }
        else if (0 == res)
        {
            // 客户端掉线
            printf("[%s : %d] 已掉线\n", inet_ntoa(client_info.sin_addr),
                   ntohs(client_info.sin_port));
            break;
        }
        // 将消息输出
        printf("[%s : %d][message : %s]\n", inet_ntoa(client_info.sin_addr),
               ntohs(client_info.sin_port), buff);

        // 判断客户端是否断开链接
        if (!strcmp(buff, "exit"))
        {
            printf("[%s : %d] 断开链接\n", inet_ntoa(client_info.sin_addr),
                   ntohs(client_info.sin_port));
            break;
        }

        strcat(buff, "---");
        // 处理消息(可以是回复)
        if (send(new_sfd, buff, sizeof(buff), 0) < 0)
        {
            ERR_MSG("send");
            break;
        }
    }
    // 关闭文件描述符
    close(new_sfd);
    return 0;
}

二、 TCP并发服务器的多线程实现

c 复制代码
#include <my_head.h>

#define SERVER_IP "192.168.125.11"      //  服务器IP
#define SERVER_PORT 6666                //  服务器端口

struct client_information
{
    int new_sfd;
    struct sockaddr_in info;
};

// 线程处理客户端信息函数
void *thread1(void *arg);

int main(int argc, const char *argv[])
{
    // 创建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("sfd");
        return -1;
    }

    // 服务器信息
    struct sockaddr_in server_info;
    server_info.sin_addr.s_addr = inet_addr(SERVER_IP);
    server_info.sin_port = htons(SERVER_PORT);
    server_info.sin_family = AF_INET;

    // 绑定
    if (bind(sfd, (struct sockaddr *)&server_info, sizeof(server_info)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }

    // 监听
    if (listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }

    // 客户端信息
    struct client_information client_info;
    socklen_t len = sizeof(client_info.info);
    // 子线程号
    pthread_t tid;
    while (1)
    {
        // 连接accept
        client_info.new_sfd = accept(sfd, (struct sockaddr *)&(client_info.info), &len);
        if (client_info.new_sfd < 0)
        {
            ERR_MSG("accept");
            return -1;
        }
        printf("[%s : %d]已连接 new_fd = %d \n", inet_ntoa(client_info.info.sin_addr),
               ntohs(client_info.info.sin_port), client_info.new_sfd);

        // 创建子线程
        if (pthread_create(&tid, NULL, thread1, &client_info) < 0)
        {
            fprintf(stderr, "线程创建出错  __%d__\n", __LINE__);
            return -1;
        }
        // 将线程分离,待线程结束后可以直接被操作系统回收
        pthread_detach(tid);
    }

    // 关闭套接字
    close(client_info.new_sfd);
    close(sfd);
    return 0;
}

// 线程处理客户端信息函数功能实现
void *thread1(void *arg)
{
    struct client_information client_info = *((struct client_information *)arg);
    ssize_t res = -1;
    char buff[128];

    while (1)
    {
        bzero(buff, sizeof(buff));
        // 接收客户端传来的信息
        res = recv(client_info.new_sfd, buff, sizeof(buff), 0);
        if (res < 0)
        {
            ERR_MSG("recv");
            break;
        }
        else if (0 == res)
        {
            // 客户端掉线
            printf("[%s : %d] 已掉线\n", inet_ntoa(client_info.info.sin_addr),
                   ntohs(client_info.info.sin_port));
            break;
        }

        /*
         * 处理回复 */
        // 将消息输出
        printf("[%s : %d][message : %s]\n", inet_ntoa(client_info.info.sin_addr),
               ntohs(client_info.info.sin_port), buff);

        // 判断客户端是否退出
        if (!strcmp(buff, "exit"))
        {
            printf("[%s : %d] 断开链接\n", inet_ntoa(client_info.info.sin_addr),
                   ntohs(client_info.info.sin_port));
            break;
        }

        strcat(buff, "---");
        // 处理消息(可以是回复)
        if (send(client_info.new_sfd, buff, sizeof(buff), 0) < 0)
        {
            ERR_MSG("send");
            break;
        }
    }
    // 关闭文件描述符
    close(client_info.new_sfd);
    // 退出线程
    pthread_exit(NULL);
}
相关推荐
wdfk_prog1 小时前
嵌入式面试真题第 10 题:高优化等级下共享状态可见性、内存模型与系统级同步设计
java·linux·开发语言·面试·职场和发展·架构·c
x-cmd3 小时前
Mac 涨价后,本地 AI 还能千元入门吗?
linux·人工智能·macos·ai·agent·amd·本地ai入门
楷哥爱开发3 小时前
如何使用 Claude Fable 5 进行网页抓取?2026最新实战教程
大数据·网络·人工智能
wdfk_prog3 小时前
嵌入式面试真题第 11 题:RTOS 优先级翻转与实时任务阻塞的通用治理
c语言·缓存·面试·职场和发展·架构
Sirius Wu4 小时前
OpenClaw Skill:Matplotlib 可视化技能 + 沙箱双层隔离完整详解
服务器·网络·人工智能·安全·ai·架构·aigc
闻道且行之4 小时前
TurboOCR:基于PP-OCRv6的极速Windows离线OCR工具,深度解析3.4GB依赖背后的技术架构
c++·人工智能·python·qt·机器学习·ocr
酣大智4 小时前
常用 IP 协议号大全(IP 层承载的上层协议)
网络·网络协议·tcp/ip
J-Tony114 小时前
【Redis】 Redis 是单线程还是多线程
服务器·数据库·mysql
c238566 小时前
Bug 猎手入门指南
c++·算法·bug
大公产经晚间消息7 小时前
《寻访独角兽》首期走进太仓,探访小科智行的硬科技“突围”
网络·人工智能·科技