MENU

多进程和多线程服务器

多进程服务器代码:

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

//多进程服务器 子进程处理连接请求

//信号触发的回调函数
void recyle(int num){
    pid_t pid;
    while((pid = waitpid(-1, NULL, WNOHANG)) > 0){
        printf("child died, pid = %d\n", pid);
    
    }
}

int main(int argc, const char *argv[]){
    if(argc < 2){
        printf("eg: ./a.out port\n");
        exit(1);
    }

    struct sockaddr_in serv_addr;
    socklen_t serv_len = sizeof(serv_addr);
    int port = atoi(argv[1]);

    //创建套接字
    int lfd = socket(AF_INET, SOCK_STREAM, 0);
    //初始化服务器 sockaddr_in
    memset(&serv_addr, 0, serv_len);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    bind(lfd, (struct sockaddr *)&serv_addr, serv_len);

    listen(lfd, 36);
    printf("Start accept ......\n");

    struct sigaction act;
    act.sa_handler = recyle;
    act.sa_flags = 0;
    sigemptyset(&act.sa_mask);
    sigaction(SIGCHLD, &act, NULL);


    struct sockaddr_in client_addr;
    socklen_t cli_len = sizeof(client_addr);

    while(1){
        //父进程接受连接请求
        //accept阻塞时若被信号中断,处理信号对应操作后
        //回来之后不阻塞,直接返回-1,这时errno = EINTR
        int cfd = accept(lfd, (struct sockaddr *)&client_addr, &cli_len);
        if(cfd == -1 && errno == EINTR){
            cfd = accept(lfd, (struct sockaddr *)&client_addr, &cli_len);
            //perror("accept error");
            //exit(1);
        }
        printf("connect successful\n");
        pid_t pid = fork();

        if(pid == 0){
            //child process 通信
            close(lfd);
            char ip[64];
            while(1){
                printf("client IP:%s, prot:%d\n", inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, ip, sizeof(ip)), ntohs(client_addr.sin_port));
                char buf[1024];
                int len = read(cfd, buf, sizeof(buf));

                if(len == -1){ //出现错误
                    perror("read error");
                    exit(1);
                }else if(len == 0){ //客户端断开链接
                    printf("client closed connect\n");
                    close(cfd);
                    break;
                }else{ //正常通信
                    printf("recv buf: %s\n", buf);
                    write(cfd, buf, len);
                }
            }
            //干掉子进程
            exit(1);

        }else if(pid > 0){
            //parent process
            close(cfd);
            //回收子进程资源 通过信号
        }
    }

    close(lfd);
    return 0;
}

多线程服务器代码:

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

//自定义线程处理函数传参结构
typedef struct sockinfo{
    pthread_t id;
    int fd;
    struct sockaddr_in addr;
}SockInfo;

void *worker(void *arg){
    SockInfo * info = (SockInfo *)arg;
    char ip[64];    //存放ip网络字节序转换主机字节序的地址
    char buf[1024]; //数据交换的buf
    while(1){
        //打印客户端ip 和 端口
        printf("client IP:%s, prot:%d\n", inet_ntop(AF_INET, &info->addr.sin_addr.s_addr, ip, sizeof(ip)), ntohs(info->addr.sin_port));

        //通信
        int len = read(info->fd, buf, sizeof(buf));
        if(len == -1){ //出现错误
            perror("read error");
            pthread_exit(NULL);
        }else if(len == 0){ //客户端断开链接
            printf("client closed connect\n");
            close(info->fd);
            break;
        }else{ //正常通信
            printf("recv buf: %s\n", buf);
            write(info->fd, buf, len);
        }
    }
    //干掉子进程
    pthread_exit(NULL);
    return NULL;
}


int main(int argc, const char *argv[]){
    if(argc < 2){
        printf("eg: ./a.out port\n");
        exit(1);
    }

    struct sockaddr_in serv_addr;
    socklen_t serv_len = sizeof(serv_addr);
    int port = atoi(argv[1]);

    //创建套接字
    int lfd = socket(AF_INET, SOCK_STREAM, 0);
    //初始化服务器 sockaddr_in
    memset(&serv_addr, 0, serv_len);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    bind(lfd, (struct sockaddr *)&serv_addr, serv_len);

    listen(lfd, 36);
    printf("Start accept ......\n");

    socklen_t cli_len = sizeof(struct sockaddr_in);

    //定义至while外防止下一个线程连接后 之前的sock[i].fd栈生命周期结束
    SockInfo info[256];
    int i = 0;
    //文件描述符初始化-1
    for(int k = 0; k < sizeof(info)/sizeof(info[0]); ++k){
        info[k].fd = -1;
    }
    while(1){
        //选择未使用的最小fd
        for(i = 0; i < 256; ++i){
            if(info[i].fd == -1){
                break;
            }
        }
        if(i == 256){
            break;
        }
        //主线程 等待接受链接请求
        info[i].fd = accept(lfd, (struct sockaddr *)&info[i].addr, &cli_len);
        if(info[i].fd == -1){
            perror("accept error");
            exit(1);
        }

        //创建子线程 通信
        pthread_create(&info[i].id, NULL, worker, &info[i]);
        //设置子线程分离
        pthread_detach(info[i].id);
    }
    close(lfd);
    //只退出主线程
    pthread_exit(NULL);
}
无标签
Archives Tip
QR Code for this page
Tipping QR Code