2022-10-24

这篇博客展示了两种不同的方法来实现TCP服务器:多进程和多线程。在多进程方案中,服务器接收连接后为每个客户端创建一个新进程进行通信。而在多线程方案中,主线程接收连接,并创建子线程来处理客户端请求。两种方法都用于并发处理客户端连接,但实现方式有所不同,多进程每个进程独立,而多线程共享资源。
摘要由CSDN通过智能技术生成

 1.多进程

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#define PORT 8000
#define IP "192.168.126.140"
typedef void (*sighandler_t)(int);
int chat(int newfd);
#define PRINT_ERR(msg) \
    do                 \
    {                  \
        perror("msg"); \
        return -1;     \
    } while (0)

void handler(int sig)
{
    while(waitpid(-1,NULL,WNOHANG) > 0);
    return;
}

int main(int argc, const char *argv[])
{
    int sig;
    signal(17,handler);
    pid_t pid;
    int sfd = socket(AF_INET, SOCK_STREAM, 0); // 创建 socket 套接字;
    if (sfd == -1)
        PRINT_ERR("socket");

    struct sockaddr_in sin; // 地址结构体
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    if (0 > bind(sfd, (struct sockaddr *)&sin, sizeof(sin))) // 绑定自己 IP 和 端口
        PRINT_ERR("bind");

    if (listen(sfd, 100)) // 单次最多发送 100 个长度
        PRINT_ERR("listen");

    int newfd;
    while (1)
    {
        newfd = accept(sfd, NULL, NULL); // 开启一块新的空间和其描述符
        if (-1 == newfd)
            PRINT_ERR("accept");
        else
            printf("链接已建立\n");
        pid = fork();
        if (pid < 0)
        {
            PRINT_ERR("fork");
        }
        else if (pid == 0)
        {
            close(sfd);
            chat(newfd);
            exit(0);
        }
        else if (pid > 0)
        {
            close(newfd);
        }
    }

    close(sfd);
    return 0;
}

int chat(int newfd)
{
    while (1)
    {
        char buff[100] = {0}; // 接受和打印消息的变量
        int res;
        while (res = recv(newfd, &buff, sizeof(buff), 0))
        {
            if (-1 == res)
                PRINT_ERR("recv");
            // printf("res = %d\n",res);
            printf("%s\n", buff);
            send(newfd, buff, sizeof(buff), 0);
        }
    }
}

2.多线程

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <pthread.h>
#define PORT 8000
#define IP "192.168.126.140"
void *chat(void *arg);
#define PRINT_ERR(msg) \
    do                 \
    {                  \
        perror("msg"); \
        return -1;     \
    } while (0)

typedef struct cli_info
{
    int newfd;

} cli_info;

int main(int argc, const char *argv[])
{
    pthread_t tid;
    int sfd = socket(AF_INET, SOCK_STREAM, 0); // 创建 socket 套接字;
    if (sfd == -1)
        PRINT_ERR("socket");

    struct sockaddr_in sin; // 地址结构体
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    if (0 > bind(sfd, (struct sockaddr *)&sin, sizeof(sin))) // 绑定自己 IP 和 端口
        PRINT_ERR("bind");

    if (listen(sfd, 100)) // 单次最多发送 100 个长度
        PRINT_ERR("listen");

    int newfd;
    while (1)
    {
        newfd = accept(sfd, NULL, NULL); // 开启一块新的空间和其描述符
        if (pthread_create(&tid, NULL, chat, &newfd) < 0)
            PRINT_ERR("pthread_create");
    }
    close(sfd);
    return 0;
}

void *chat(void *arg)
{
    pthread_detach(pthread_self());
    int newfd = *(int *)arg;
    char buff[100] = {0}; // 接受和打印消息的变量
    int res;
    while (res = recv(newfd, &buff, sizeof(buff), 0))
    {
        if (-1 == res)
        {
            perror("子线程接受失败\n");
            close(newfd);
            pthread_exit(NULL);
        }
        // printf("res = %d\n",res);
        printf("%s\n", buff);
        send(newfd, buff, sizeof(buff), 0);
    }
    close(newfd);
    pthread_exit(NULL);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值