网络编程套接字(6)——多进程版本的简单TCP网络程序

在上一篇博客中,我们实现了单进程版本的简单TCP网络程序。这种程序一次只能连接一个客户端,具有局限性。所以现在我们来实现多进程版本的简单TCP网络程序,通过创建子进程的方式来支持多连接。

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

#define MAX 128

int Startup(char* ip,int port){
    int sock = socket(AF_INET,SOCK_STREAM,0);
    if(sock < 0){
        printf("socket error!\n");
        exit(2);
    }
    struct sockaddr_in local;
    local.sin_family = AF_INET;
    local.sin_addr.s_addr = inet_addr(ip);
    local.sin_port = htons(port);

    if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0){
        printf("bind error!\n");
        exit(3);
    }
    
    if(listen(sock,5) < 0){
        printf("listen error!\n");
        exit(4);
    }

    return sock;
}

void service(int sock,char* ip,int port){
    char buf[MAX];
    while(1){
        buf[0] = 0;
        ssize_t s = read(sock,buf,sizeof(buf)-1);
        if(s > 0){
            buf[s] = 0;
            printf("[%s:%d] say# %s\n",ip,port,buf);
            write(sock,buf,strlen(buf));
        }
        else if(s == 0){
            printf("client [%s:%d] quit!\n",ip,port);
            break;
        }
        else{
            printf("read error!\n");
            break;
        }
    }
}

int main(int argc,char* argv[]){
    if(argc != 3){
        printf("Usage:%s [ip] [port]\n",argv[0]);
        return 1;
    }
    int listen_sock = Startup(argv[1],atoi(argv[2]));
    
    struct sockaddr_in peer;
    char ipBuf[24];
    for(;;){
        ipBuf[0] = 0;
        socklen_t len = sizeof(peer);
        int new_sock = accept(listen_sock,(struct sockaddr*)&peer,&len);
        if(new_sock < 0){
            printf("accept error!\n");
            continue;
        }
        inet_ntop(AF_INET,(const void*)&peer.sin_addr,ipBuf,sizeof(ipBuf));
        int p = ntohs(peer.sin_port);
        printf("get a new connect,[%s:%d]\n",ipBuf,p);

        pid_t id = fork();
        if(id == 0){//child
            close(listen_sock);
            if(fork() > 0){
                exit(0);
            }
            service(new_sock,ipBuf,p);
            close(new_sock);
            exit(0);
        }
        else if(id > 0){//father
            close(new_sock);
            waitpid(id,NULL,0);
        }
        else{
            printf("fork error!\n");
            continue;
        }
    }
    return 0;
}
client.c:
#include <stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<stdlib.h>
#include<string.h>

#define MAX 128

int main(int argc,char* argv[]){
    if(argc != 3){
        printf("Usage:%s [ip] [port]\n",argv[0]);
        return 1;
    }
    int sock = socket(AF_INET,SOCK_STREAM,0);
    if(sock < 0){
        printf("socket error!\n");
        return 2;
    }

    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(argv[2]));
    server.sin_addr.s_addr = inet_addr(argv[1]);

    if(connect(sock,(struct sockaddr*)&server,sizeof(server)) < 0){
        printf("connect error!\n");
        return 3;
    }

    char buf[MAX];
    while(1){
        printf("please Enter# ");
        fflush(stdout);
        ssize_t s = read(0,buf,sizeof(buf)-1);
        if(s > 0){
        buf[s-1] = 0;
        if(strcmp("quit",buf) == 0){
            printf("client quit!\n");
            break;
        }
        write(sock,buf,strlen(buf));
        s = read(sock,buf,sizeof(buf)-1);
        buf[s] = 0;
        printf("server Echo# %s\n",buf);
        }
    }
    close(sock);
    return 0;
}

接收请求时,先由服务器创建子进程,然后子进程又创建一个孙子进程,由孙子进程完成和客户端的交互。

  • 多进程版本的优缺点

优点:

    (1)能处理多用户请求;

    (2)代码较为简单,编写周期短;

    (3)由于进程具有独立性,所以多进程服务器的稳定性强。

缺点:

    (1)连接到来时才创建子进程,而创建子进程需要浪费时间,会影响性能;

    (2)多进程服务器的每个进程都占用资源,进而导致其能服务的客户数量有限;

    (3)多进程服务器随着进程数量的增多,CPU的压力会变大,CPU调度所需的时间会变长,会影响性能。




猜你喜欢

转载自blog.csdn.net/cecilia3333/article/details/80217623
今日推荐