高并发服务器-使用多进程(Multi-Process)实现【C语言】

在上期的socket套接字的使用详解中(socket套接字的使用详解)最后实现的TCP服务器只能处理一个客户端的请求发送,当有其他客户端请求连接时会被阻塞。为了能同时处理多个客户端的连接请求,本期使用多进程的方式来解决。

解决方案步骤总结

  1. 初始化服务器

    • 创建监听套接字(socket)。
    • 绑定套接字到指定地址和端口(bind)。
    • 开始监听连接请求(listen)。
  2. 等待连接

    • 进入一个无限循环,等待并接受客户端连接(accept)。
  3. 创建子进程

    • 每当接受到一个新的客户端连接,创建一个子进程(fork)。
    • 子进程负责与客户端通信,处理请求并发送响应。
  4. 父进程继续监听

    • 父进程关闭与客户端通信的套接字,继续监听新的连接请求。
  5. 处理子进程结束信号(可选)

    • 注册信号处理函数,处理子进程结束信号,避免僵尸进程。

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include <ctype.h>

// 处理SIGCHLD信号,避免僵尸进程
void sigchld_handler(int signo) {
    while (waitpid(-1, NULL, WNOHANG) > 0); //表示非阻塞地等待任意子进程终止。-1 表示等待任何子进程,NULL 表示不需要子进程的退出状态,WNOHANG 表示非阻塞。
}

// 处理客户端通信
void handle_client(int cfd) {
    char buf[1024];
    int n;
    while ((n = read(cfd, buf, sizeof(buf))) > 0) {
        for (int i = 0; i < n; i++) {
            buf[i] = toupper(buf[i]);
        }
        write(cfd, buf, n);
    }
    close(cfd);
}

int main() {
    // 创建监听套接字
    int lfd = socket(AF_INET, SOCK_STREAM, 0);
    if (lfd < 0) {
        perror("socket error");
        return -1;
    }

    // 绑定套接字
    struct sockaddr_in serv;
    bzero(&serv, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_port = htons(8888);
    serv.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(lfd, (struct sockaddr *)&serv, sizeof(serv)) < 0) {
        perror("bind error");
        return -1;
    }

    // 监听连接请求
    listen(lfd, 3);

    // 设置SIGCHLD信号处理
    struct sigaction sa;
    sa.sa_handler = sigchld_handler;
    sigemptyset(&sa.sa_mask);           // 初始化信号屏蔽字为空。
    sa.sa_flags = SA_RESTART;           //设置信号处理之后自动重新启动被信号打断的系统调用。
    if (sigaction(SIGCHLD, &sa, NULL) < 0) {
        perror("sigaction error");
        return -1;
    }

    while (1) {
        struct sockaddr_in client;
        socklen_t len = sizeof(client);
        int cfd = accept(lfd, (struct sockaddr *)&client, &len);
        if (cfd < 0) {
            perror("accept error");
            continue;
        }

        // 打印客户端连接信息
        char sIP[16];
        memset(sIP, 0x00, sizeof(sIP));
        printf("Client connected: IP [%s], PORT [%d]\n", inet_ntop(AF_INET, &client.sin_addr.s_addr, sIP, sizeof(sIP)), ntohs(client.sin_port));

        pid_t pid = fork();
        if (pid == 0) { // 子进程
            close(lfd); // 子进程关闭监听套接字
            handle_client(cfd); // 处理客户端通信
            printf("Client disconnected: IP [%s], PORT [%d]\n", inet_ntop(AF_INET, &client.sin_addr.s_addr, sIP, sizeof(sIP)), ntohs(client.sin_port));
            exit(0); // 子进程处理完成后退出
        } else if (pid > 0) { // 父进程
            close(cfd); // 父进程关闭与客户端通信的套接字
        } else {
            perror("fork error");
            close(cfd);
        }
    }

    close(lfd);
    return 0;
}

客户端:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define PORT 8888
#define BUFFER_SIZE 1024
#define SERVER_IP "127.0.0.1"

int main() {
    int sock = 0, valread;
    struct sockaddr_in serv_addr;
    char buffer[BUFFER_SIZE] = {0};
    char input_buffer[BUFFER_SIZE] = {0};
    char *hello = "Hello from client";
    int opt = 1;

    // 创建 TCP 套接字
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket creation failed");
        return -1;
    }

    // 设置服务器地址结构
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    // 将 IPv4 地址从文本转换为二进制形式
    if (inet_pton(AF_INET, SERVER_IP, &serv_addr.sin_addr) <= 0) {
        perror("Invalid address/ Address not supported");
        return -1;
    }

    // 连接服务器
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("Connection Failed");
        return -1;
    }

    printf("Connected to server\n");

    // 循环发送消息并接收响应
    while (1) {
        printf("Enter message to send (or 'exit' to quit): ");
        fgets(input_buffer, BUFFER_SIZE, stdin);

        // 去掉输入的换行符
        input_buffer[strcspn(input_buffer, "\n")] = 0;

        // 如果输入是 'exit',则退出循环
        if (strcmp(input_buffer, "exit") == 0) {
            break;
        }

        // 发送消息给服务器
        send(sock, input_buffer, strlen(input_buffer), 0);
        printf("Message sent to server: %s\n", input_buffer);

        // 接收服务器的响应
        valread = read(sock, buffer, BUFFER_SIZE);
        printf("Server response: %s\n", buffer);
        memset(buffer, 0, sizeof(buffer));
    }

    close(sock);
    return 0;
}

相关推荐

  1. 并发服务器-进程

    2024-07-17 19:20:06       58 阅读
  2. 进程并发服务器

    2024-07-17 19:20:06       37 阅读
  3. C语言进程服务器

    2024-07-17 19:20:06       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 19:20:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 19:20:06       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 19:20:06       58 阅读
  4. Python语言-面向对象

    2024-07-17 19:20:06       69 阅读

热门阅读

  1. vue3中常用组件封装及使用

    2024-07-17 19:20:06       21 阅读
  2. SpringBoot+HttpClient实现文件上传下载

    2024-07-17 19:20:06       22 阅读
  3. 根据语义切分视频

    2024-07-17 19:20:06       17 阅读
  4. 量化交易对市场波动的反应机制

    2024-07-17 19:20:06       19 阅读
  5. Html_Css问答集(11)

    2024-07-17 19:20:06       18 阅读
  6. 最全—航班信息管理系统【数组版】

    2024-07-17 19:20:06       17 阅读
  7. 什么是HTTP协议攻击

    2024-07-17 19:20:06       20 阅读
  8. AnyConv OGG 转换器:轻松转换音频格式

    2024-07-17 19:20:06       24 阅读