网络编程_Day04多进程/多线程实现TCP服务器

多进程/多线程实现TCP服务器

process

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



#define PORT 9999
#define SERVER_IP "192.168.31.165"
int deal_cli_msg(int new_sfd, struct sockaddr_in cin);

#define ERR_MSG(msg) do \
{ \
    fprintf(stderr, "line:%d ", __LINE__); \
    perror(msg); \
}while(0);
void zombie_callBack(int sig)
{
    while(waitpid(-1, NULL, WNOHANG) > 0);
}

int main(int argc, const char *argv[])
{
    //捕获17号信号
    __sighandler_t s = signal(SIGCHLD, zombie_callBack);
    if(SIG_ERR == s)
    {
        ERR_MSG("signal");
        return -1;
    }
    
    //创建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("socket created: %d\n", sfd);


    //设置服务器套接字文件:端口快速可重用
    int reuse = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("SO_REUSEADDR success\n");


    //为服务器套接子文件描述符绑定 IP 和 PORT
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(SERVER_IP);
    if(bind(sfd, (struct sockaddr*)&sin, sizeof(struct sockaddr_in)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("bind() success\n");


    //设置套接字为监听状态
    if(listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }
    printf("listen success\n");

    //为连接好的客户端创建新的套接字文件描述符
    struct sockaddr_in cin;
    socklen_t addr_len = sizeof(cin);
    int new_sfd = -1;
    pid_t cpid = -1;

    while(1)
    {
        //为连接好的客户端创建新的套接字文件描述符
        new_sfd = accept(sfd, (struct sockaddr *)&cin, &addr_len);
        if(new_sfd < 0)
        {
            ERR_MSG("accept");
            return -1;
        }
        printf("新客户端连接成功:[%s : %d] newfd=%d\n", \
                inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), new_sfd);


        //创建子进程
        if( 0 == (cpid = fork()) )//子进程
        {
            close(sfd);
            deal_cli_msg(new_sfd, cin);

            close(new_sfd);
            exit(0);//客户端通信完毕,退出子进程
        }
        //父进程
        else if(cpid > 0)
        {
            close(new_sfd); //对于父进程而言,newfd没有用
        }
        else
        {
            ERR_MSG("fork");
            return -1;
        }
        

    }
    close(sfd);
    return 0;
}

int deal_cli_msg(int new_sfd, struct sockaddr_in cin)
{
    char buf[512] = "";
    ssize_t res = 0;
    while(1)
    {
        bzero(buf, sizeof(buf));

        //接收数据
        res = recv(new_sfd, (void*)&buf, sizeof(buf), 0);
        //接收出错
        if(res < 0)
        {
            ERR_MSG("recv");
            return -1;
        }
        //客户端关闭
        else if(0 == res)
        {
            fprintf(stderr, "[%s : %d] new_sfd=%d 客户端下线\n", \
                inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), new_sfd);
            break;
        }
        //res>0正常接收数据
        printf("[%s : %d] newfd=%d, data: %s\n", \
                inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), new_sfd, buf);

        strcat(buf, "*_*\n");
        if(send(new_sfd, buf, sizeof(buf), 0) < 0)
        {
            ERR_MSG("send");
            return -1;
        }
        printf("send success\n");
    }
    return 0;
}

thread

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


#define PORT 9999
#define SERVER_IP "192.168.31.165"
void * deal_cli_msg(void * arg);
struct cli_arg
{
    int new_sfd;
    struct sockaddr_in cin;
};
#define ERR_MSG(msg) do \
{ \
    fprintf(stderr, "line:%d ", __LINE__); \
    perror(msg); \
}while(0);
int main(void)
{
    //创建流式套接字
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("socket created: %d\n", sfd);


    //设置服务器套接字文件:端口快速可重用
    int reuse = 1;
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("SO_REUSEADDR success\n");


    //为服务器套接子文件描述符绑定 IP 和 PORT
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(SERVER_IP);
    if(bind(sfd, (struct sockaddr*)&sin, sizeof(struct sockaddr_in)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("bind() success\n");


    //设置套接字为监听状态
    if(listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }
    printf("listen success\n");

    //为连接好的客户端创建新的套接字文件描述符
    struct sockaddr_in cin;
    socklen_t addr_len = sizeof(cin);
    int new_sfd = -1;
    pthread_t tid;
    struct cli_arg arg;

    while(1)
    {
        //为连接好的客户端创建新的套接字文件描述符
        new_sfd = accept(sfd, (struct sockaddr *)&cin, &addr_len);
        if(new_sfd < 0)
        {
            ERR_MSG("accept");
            return -1;
        }
        printf("新客户端连接成功:[%s : %d] newfd=%d\n", \
                inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), new_sfd);


        //创建分支线程
        arg.cin = cin;
        arg.new_sfd = new_sfd;
        if(pthread_create(&tid, NULL, deal_cli_msg, (void *)&arg) != 0)
        {
            fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
            return -1; 
        }
        pthread_detach(tid);        //分离线程
        

    }
    close(sfd);
    return 0;
}

void * deal_cli_msg(void * arg)
{
    char buf[512] = "";
    ssize_t res = 0;
    int new_sfd = ((struct cli_arg*)arg)->new_sfd;
    struct sockaddr_in cin = ((struct cli_arg*)arg)->cin;
    while(1)
    {
        bzero(buf, sizeof(buf));

        //接收数据
        res = recv(new_sfd, (void*)&buf, sizeof(buf), 0);
        //接收出错
        if(res < 0)
        {
            ERR_MSG("recv");
            break;
        }
        //客户端关闭
        else if(0 == res)
        {
            fprintf(stderr, "[%s : %d] new_sfd=%d 客户端下线\n", \
                inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), new_sfd);
            break;
        }
        //res>0正常接收数据
        printf("[%s : %d] newfd=%d, data: %s\n", \
                inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), new_sfd, buf);

        strcat(buf, "*_*\n");
        if(send(new_sfd, buf, sizeof(buf), 0) < 0)
        {
            ERR_MSG("send");
            break;
        }
        printf("send success\n");
    }
    close(new_sfd);
    pthread_exit(NULL);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值