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

2023-09-21 21:11:29

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

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

代码

#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并发服务器的多线程实现

#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);
}
更多推荐

91. 面试官:JSONP的原理是什么?

91期1.JSONP的原理是什么?2.css优先级是什么样的?3.display的值有哪些,分别有什么作用?上面问题的答案会在第二天的公众号(程序员每日三问)推文中公布90期问题及答案1.什么是前后端分离,好处是什么?前后端分离是一种软件架构模式,它将前端和后端开发分离为两个独立的工作流程和技术栈,它们通过API或We

Java 中 jps 命令

jps(JavaVirtualMachineProcessStatusTool)是Java开发工具包(JDK)中的一个命令行工具,用于查看Java虚拟机(JVM)中运行的Java进程的状态信息。它通常用于检查正在运行的Java应用程序的进程ID(PID)和相关信息,这对于调试和性能监控非常有用。以下是jps命令的使用示

LeetCode 之 长度最小的子数组

算法模拟:AlgorithmVisualizer在线工具:C++在线工具如果习惯性使用VisualStudioCode进行编译运行,需要C++11特性的支持,可参考博客:VisualStudioCode支持C++11插件配置长度最小的子数组LeetCode长度最小的子数组问题:给定一个含有n个正整数的数组和一个正整数t

CAN总线物理层

本文的目的并不是为了介绍或普及CAN总线相关知识,而是为了了解CAN总线,进而为CAN通信一致性测试做知识储备。CAN,控制器局域网,全称:ControllerAreaNetwork。1986年,由德国Bosch公司为汽车开发的网络技术,主要用于汽车的监测与控制,目的为适应汽车“减少线束的数量”、“通过多个网络进行大量

vue学习-03vue父子组件与ref属性

本篇开始,我们将复习一下上篇的组件引入:App.vue<template><div><imgsrc="./assets/logo.png"alt="logo"><!--编写组件标签--><School></School><Student></Student></div></template><script>//引入组件

CSS中的定位

position的属性与含义CSS中的position属性用于控制元素在页面中的定位方式,有四个主要的取值,每个取值都会影响元素的布局方式,它们是:static(默认值):这是所有元素的初始定位方式。在静态定位下,元素会按照它们在文档流中的顺序依次排列,不受top、right、bottom、left等属性的影响。静态定

c++11相关内容介绍

前言在2003年C++标准委员会曾经提交了一份技术勘误表(简称TC1),使得C++03这个名字已经取代了C++98称为C++11之前的最新C++标准名称。不过由于C++03(TC1)主要是对C++98标准中的漏洞进行修复,语言的核心部分则没有改动,因此人们习惯意性的将两个标准合并称为C++98/03标准。从C++0x到

力扣669 补9.16

最近大三上四天有早八,真的是受不了了啊,欧嗨呦,早上困如狗,然后,下午困如狗,然后晚上困如狗,尤其我最近在晚上7点到10点这个时间段看力扣,看得我昏昏欲睡,不自觉就睡了1个小时,可能在犯困的时间段不适合做高难度的题目。还有英语也要抓紧搞,好久没搞英语了,力扣每天一题就搞得我没时间搞别的了,基本就是下午在看力扣,然后睡了

基于Spring Boot的房屋租赁系统

目录前言一、技术栈二、系统功能介绍租客功能模块的实现​编辑管理员功能模块的实现三、核心代码1、登录模块2、文件上传模块3、代码封装前言房屋是人类生活栖息的重要场所,随着城市中的流动人口的增多,人们对房屋租赁需求越来越高,为满足用户查询房屋、预约看房、房屋租赁的需求,特开发了本基于SpringBoot的房屋租赁系统。本文

Android Studio导入aosp源码

1、在Ubuntu系统下,进入源码根目录,运行如下命令:sourcebuild/envsetup.sh#初始化环境变量。lunchsdk_phone_x86_64makeidegen-j6#六核编译idegen模块忽略此命令:mmmdevelopment/tools/idegen/#此命令可以不执行(同makeideg

【leetcode】数组排序

【leetcode】数组排序task03主要了解了数组中常见的排序方法:1.常见数组排序方法冒泡排序(BubbleSort):冒泡排序是一种简单的排序算法,它多次遍历数组,比较相邻的元素并交换它们,直到整个数组按升序排列。时间复杂度为O(n^2)。选择排序(SelectionSort):选择排序是一种简单的排序算法,它

热文推荐