网络编程(day4)——TCP并发服务器模型

多进程并发服务器模型

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

#define ERR_MSG(msg)                              \
    do {                                          \
        fprintf(stderr, "line : %d\n", __LINE__); \
        perror(msg);                              \
    } while (0)

#define IP "192.168.31.252"
#define PORT 8888

void Deal_Client_Msg(int newfd, struct sockaddr_in cin);

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

int main(int argc, char const* argv[])
{
    // 捕捉SIGCHLD信号,回收僵尸进程
    if (signal(SIGCHLD, handler) == SIG_ERR) {
        ERR_MSG("signal error");
        return -1;
    }

    // 创建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd < 0) {
        ERR_MSG("socket error");
        return -1;
    }
    printf("socket successful  __%d__\n", __LINE__);

    // 设置端口重复使用端口
    int reuse = 1;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
        ERR_MSG("setsockopt error");
        return -1;
    }
    printf("允许端口快速重用!\n");

    //(必须)绑定服务器IP地址和端口
    // 填充地址信息结构体
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    // 绑定
    if (bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
        ERR_MSG("bind error");
        return -1;
    }
    printf("bind successful  __%d__\n", __LINE__);

    // 将套接字设置为被动监听状态
    if (listen(sfd, 128) < 0) {
        ERR_MSG("listen error");
        return -1;
    }
    printf("listen successful  __%d__\n", __LINE__);

    // 存储客户端的地址信息结构体
    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);
    int newfd; // accept返回的文件描述符
    pid_t pid; // 进程号

    while (1) {
        // 从连接成功的队列头获取一个客户端的地址信息结构体
        // 父进程负责与客户端建立连接
        newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
        if (newfd < 0) {
            ERR_MSG("accept error");
            return -1;
        }
        printf("from client-[%s : %d] newfd = %d 连接成功  __%d__\n",
            inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);

        // 执行到此,说明已经有客户端与服务器建立连接
        // 创建子进程负责与客户端通信
        pid = fork();
        if (pid > 0) // 父进程,负责连接
        {
            close(newfd);
        } else if (0 == pid) {
            close(sfd);
            Deal_Client_Msg(newfd, cin);
            close(newfd);
            exit(EXIT_SUCCESS);

        } else {
            ERR_MSG("fork error");
            return -1;
        }
    }

    return 0;
}

void Deal_Client_Msg(int newfd, struct sockaddr_in cin)
{
    char buf[128] = { 0 };
    ssize_t res;
    while (1) {
        // 读取数据
        bzero(buf, sizeof(buf));
        res = recv(newfd, buf, sizeof(buf), 0);
        if (res < 0) {
            ERR_MSG("recv error");
            break;
        } else if (0 == res) {
            printf("from clinet-[%s : %d] newfd = %d  客户端下线  __%d__\n",
                inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);
            break;
        }
        printf("from clinet-[%s : %d] newfd = %d  buf = %s  __%d__\n",
            inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf, __LINE__);

        // 发送数据
        strcat(buf, "__HQYJ__");
        if (send(newfd, buf, strlen(buf), 0) < 0) {
            ERR_MSG("send error");
            break;
        }
        printf("发送成功!\n");
    }
}

多线程并发服务器模型

#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

struct Client_Msg {
    int newfd;
    struct sockaddr_in cin;
};

#define ERR_MSG(msg)                              \
    do {                                          \
        fprintf(stderr, "line : %d\n", __LINE__); \
        perror(msg);                              \
    } while (0)

#define IP "192.168.31.252"
#define PORT 8888

void* Deal_Client_Msg(void* arg);

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

    // 设置端口快速重用
    int reuse = 1;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
        ERR_MSG("setsockopt error");
        return -1;
    }
    printf("允许快速重用端口! __%d__\n", __LINE__);

    //(必须)绑定服务器IP地址和端口
    // 填充地址信息结构体
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    // 绑定
    if (bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
        ERR_MSG("bind error");
        return -1;
    }
    printf("bind successful  __%d__\n", __LINE__);

    // 将套接字设置为被动监听状态
    if (listen(sfd, 128) < 0) {
        ERR_MSG("listen error");
        return -1;
    }
    printf("listen successful  __%d__\n", __LINE__);

    int newfd;
    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);
    pthread_t tid;
    struct Client_Msg info;
    while (1) {
        // 从连接成功的队列头获取一个客户端的地址信息结构体
        // 父进程负责与客户端建立连接
        newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
        if (newfd < 0) {
            ERR_MSG("accept error");
            return -1;
        }
        printf("from client-[%s : %d] newfd = %d 连接成功 __%d__\n",
            inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);

        info.newfd = newfd;
        info.cin = cin;

        // 创建线程
        if (pthread_create(&tid, NULL, Deal_Client_Msg, (void*)&info) != 0) {
            fprintf(stderr, "pthread_create error");
            return -1;
        }
        // 分离线程
        pthread_detach(tid);
    }
    close(sfd);
    return 0;
}

void* Deal_Client_Msg(void* arg)
{
    struct Client_Msg info = *(struct Client_Msg*)arg;
    char buf[128] = { 0 };
    ssize_t res;
    while (1) {
        // 接收数据
        bzero(buf, sizeof(buf));
        res = recv(info.newfd, buf, sizeof(buf), 0);
        if (res < 0) {
            ERR_MSG("recv error");
            break;
        } else if (0 == res) {
            printf("from client-[%s : %d] newfd = %d  客户端下线  __%d__\n",
                inet_ntoa(info.cin.sin_addr), ntohs(info.cin.sin_port), info.newfd, __LINE__);
            break;
        }
        printf("from client-[%s : %d] newfd = %d  buf = %s  __%d__\n",
            inet_ntoa(info.cin.sin_addr), ntohs(info.cin.sin_port), info.newfd, buf, __LINE__);

        // 发送数据
        strcat(buf, "__HQYJ__");
        if (send(info.newfd, buf, strlen(buf), 0) < 0) {
            ERR_MSG("send error");
            break;
        }
        printf("发送成功!\n");
    }
    close(info.newfd);
    pthread_exit(NULL);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值