Socket通信(三)多进程服务器的实现

Process-per-connection

一个连接由一个进程来处理并发

Socket编程(二)中的基本回射服务器,这个时候服务器只允许一个客户端对它进行连接,不允许多个客户端进行连接

因为一个客户端连接过来,服务器就处于为它服务的状态;即使再有客户端连接过来,服务器也无法接收它;我们的程序处于一个死循环的状态,没有能力去再调用一个accept来接收新的客户端。

这个时候我们就可以创建一个进程来处理这个连接。

N个连接是有N+1个进程来处理的,父进程用来处理和客户端的连接,子进程用来处理每个连接的通信细节。

服务器端需要两个套接字,一个是监听套接字sockfd,另一个是已连接套接字conn。Sockfd是用来监听,用来接收三次握手数据,一旦三次握手过程完成,那么就将它放到已连接队列,已连接队列返回一个连接即已连接套接字conn(它主要用来与客户端进行通信,是个主动套接字)。如果有N个客户端,那么服务器就有一个监听套接字sockfd和N个已连接套接字conn。

以下是在基本回射服务器的基础上,设计的多进程回射服务器。

服务端代码echo-process-server.c

#include          
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ERR_EXIT(m)

void do_service(int conn)
{
    //communication
    char recvbuf[1024];
    while(1)
    { 
        memset(recvbuf,0,sizeof(recvbuf));
        int ret=read(conn,recvbuf,sizeof(recvbuf));
        if(ret==0)//judge the client whether close or not 
        {
             printf("client close!\n");
             break;//if the client closed,break the while-round
        }
        if(ret==-1)
        {
             ERR_EXIT("read error!");//avoid endless-round,if read-error,exit 
        }
        fputs(recvbuf,stdout);
        write(conn,recvbuf,ret);
    }
}

int main(void)
{
    //create a socket
    int sockfd;
    if((sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
    /*if((sockfd=socket(AF_INET,SOCK_SCREAM,0))<0);  //SOCK_SCREAM TCP 0 */
          ERR_EXIT("socket error!");
    
    //initialize address
    struct sockaddr_in servaddr;
    memset(&servaddr,0,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_port=htons(5188);
    servaddr.sin_addr.s_addr=htonl(INADDR_ANY); //0.0.0.0 all of the host address
    /*servaddr.sin_addr.s.addr=inet_addr("127.0.0.1");*/
    /*inet_aton("127.0.0.1",&servaddr.sin_addr);*/
    
    //set address reuse,to solve the problem of TIME_WAIT
    //(when we want restart the server,it will be in the state of
    //TIME_WAIT,which make we cannot restart the server)
    //but this is not mean we can start two server to use
    int on=1;
    if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))<0)
          ERR_EXIT("setsockopt error!");

    //bind
    if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
          ERR_EXIT("bind error!"); 
    
    //listen
    if(listen(sockfd,SOMAXCONN)<0)
          ERR_EXIT("listen error!"); 

    //accept
    struct sockaddr_in peeraddr;  //define peer address
    socklen_t peerlen=sizeof(peeraddr);
    int conn;  //define a new socket,the accept will return it.

    pid_t pid;

    while(1)
    {
        
        if((conn=accept(sockfd,(struct sockaddr*)&peeraddr,&peerlen))<0)
              ERR_EXIT("accept error!");     

        //print client address and port
        printf("ip=%s port=%d\n",inet_ntoa(peeraddr.sin_addr),
               ntohs(peeraddr.sin_port));
    
        pid=fork(); //create fork
        if(pid==-1)
            ERR_EXIT("fork error!");
        if(pid==0) //subprocess to communication
        {
           close(sockfd);//subprocess do not need listened socket,so we close it
           do_service(conn);//use the function of do_service,
                            //which is defined in the start of this program
           exit(EXIT_SUCCESS);
          }
        else
        close(conn);//parent-process do not need the connected socket,
                    //so we close it
   }

    return 0;

}

echo-process-client.c客户端代码

#include          
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ERR_EXIT(m)

int main(void)
{
    //create a socket
    int sock;
    if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
    /*if((sock=socket(AF_INET,SOCK_SCREAM,0))<0);  //SOCK_SCREAM TCP 0 */
          ERR_EXIT("socket error!");
    
    //initialize address
    struct sockaddr_in servaddr;
    memset(&servaddr,0,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_port=htons(5188);
    servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
    /*inet_aton("127.0.0.1",&servaddr.sin_addr);*/
    
    //connect
    if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
          ERR_EXIT("connect error!");

    char sendbuf[1024]={0};
    char recvbuf[1024]={0};
    while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
    {
        write(sock,sendbuf,strlen(sendbuf));
        read(sock,recvbuf,sizeof(recvbuf));
        fputs(recvbuf,stdout);

        //clear the buf
        memset(sendbuf,0,sizeof(sendbuf));
        memset(recvbuf,0,sizeof(recvbuf));
    }
     
    close(sock);
    return 0;

}

 

你可能感兴趣的:(socket)