Multi-Process Network Service

Source: Internet
Author: User
Tags mul strcmp htons

1. High-performance Network Service Program

One of the advantages of Linux is that it can be used to design a variety of high performance network service programs, one of the characteristics of high performance is to achieve concurrent access processing, and simultaneously provide services to multiple online users, multi-process network services, multi-threaded network services, thread pool network services;

2. Multi-Process Network Service

: Using the parent-child process relationship in Linux system to provide concurrent services for multiple users is a popular concurrency service technology, the basic idea is: to a user, start a service process. If a new connection arrives, the child process starts interacting with it, and the child process exits automatically when the service finishes.

The model is as follows:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/87/F1/wKiom1fkg43D4_T1AAAnXS1Z9-o072.png-wh_500x0-wm_3 -wmp_4-s_3578855993.png "title=" Qq20160923092112.png "alt=" Wkiom1fkg43d4_t1aaanxs1z9-o072.png-wh_50 "/>

3, the Code implementation:

A multi-process network service is simulated with an integer operation.

(1), utili.h

#include <unistd.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys /socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <pthread.h> #define Server_ PORT 9090#define server_ip "127.0.0.1" #define Listen_queue 5#define buffer_size 255typedef enum{add,sub,mul,div,mod    , quit}oper_type;typedef struct operstruct{int op1;    int OP2; Oper_type OPER;} Operstruct;

(2), ser.c

#include ". /utili.h "Void process_handler (Int sockconn); Void process_handler (Int sockConn) {     OperStruct op;     int result;     while (1) {        int res = recv (SockConn, &op,  sizeof (OP),  0);          if (res == -1) {             printf ("recv data fail.\n");             continue;         }           if (op.oper ==  ADD) {            result = op.op1 +  op.op2;        }else if (op.oper == sub)          {                result = op.op1 - op.op2;        }else  if (Op.oper == mul) {            result  = op.op1 * op.op2;        }else if (Op.oper &NBSP;==&NBSP;DIV) {            result =  Op.op1 / op.op2;        }else if (op.oper ==  QUIT) {            break;         }           res =  Send (sockconn, &result, sizeof (result),  0);          if (RES&NBSP;==&Nbsp;-1) {            printf ("send data  fail.\n ");            continue;         }    }    close (sockConn);} Int main (void) {    int sockser = socket (Af_inet, sock_stream,  0);     if (sockser == -1) {         Perror ("socket");         return -1;    }     struct sockaddr_in addrser, addrcli;    addrser.sin _family = af_inet;    addrser.sin_port = htons (SERVER_PORT);  &NBSP;&NBSP;&NBSP;ADDRSER.SIN_ADDR.S_ADDR&NBSP;=&NBSP;INET_ADDR (SERVER_IP);     socklen_t  len = sizeof (struct sOCKADDR);     int res = bind (sockser,  (struct sockaddr*) & Addrser, len);     if (res == -1) {         perror ("bind");         close (Sockser);         return -1;          }     listen (Sockser, listen_queue);    int sockconn;     while (1) {        printf ("server wait client  connect.......\n ");         sockconn = accept (SockSer,   (struct sockaddr*) &addrcli, &len);         if ( sockconn == -1) {            printf (" server accept client connect fail.\n ");            continue;         }else{             printf ("server accept client connect success.\n");             printf ("client ip:>%s\n",  inet_ntoa (AddrCli.sin_ addr));             printf ("Client Port:>%d \ n ", Ntohs (addrcli.sin_port));        }         pid_t pid;        pid = fork ();         if (pid == 0) {             close (Sockser);             process_handler(Sockconn);             exit (0);         }else if (pid > 0) {             close (Sockconn);             continue;            }else{             printf ("create process fail.\n");             continue;         }    }    close (SockSer);     return 0;}

(3), cli.c

#include "utili.h" Void inputdata (operstruct *pt); Void inputdata (OPERSTRUCT&NBSP;*PT) {     printf ("please input op1 and op2 : ");     scanf ("%d %d", & (PT-&GT;OP1), & (PT-&GT;OP2));} Cliint main (void) {    int sockcli = socket (AF_INET, SOCK_STREAM ,  0);      if (sockcli == -1) {         perror ("socket");        return -1;      }       struct sockaddr_in addrSer;     Addrser.sin_family = af_inet;    addrser.sin_port = htons (SERVER_PORT) ;     addrser.sin_addr.s_addr = inet_addr (SERVER_IP);     Socklen_t len = sizeof (STRUCT&NBSP;SOCKADDR);    int res = connect (sockcli,  (struct sockaddr*) &addrSer,  len);     if (res == -1) {         Perror ("Connect");         close (SOCKCLI);         return -1;     }else{         printf ("client connect server success.\n");    }     char cmd[2];    OperStruct  op;     Int result;    while (1) {        printf (" please input operator :  "); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s ", cmd) ;         if (strcmp (cmd,  "+")  == 0) {             op.oper = add;             inputdata (&AMP;OP);         }else if (strcmp (cmd, "-")  ==  0) {            op.oper = sub;             inputdata (&AMP;OP);         }else if (strcmp (cmd, "*")  == 0) {             op.oper = MUL;             inputdata (&AMP;OP);         }else if (strcmp (cmd, "/")  == 0) {            op.oper = div;             inputdata (&AMP;OP);     &nBsp;   }else if (strcmp (cmd,  "quit")  == 0) {             op.oper = QUIT;         }else{                 printf ("cmd invalid.\n");        }         res = send (sockcli, &op, sizeof (OP),  0);         if (res == -1) {             printf ("send data fail.\n");             continue;        }         if (op.oper == quit)              break;  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RES&NBSP;=&NBSP;RECV (sockcli, &result, sizeof (result),  0 );         if (res == -1) {             printf ("recv data fail.\n");             continue;        }         printf ("result = %d\n",  result);     }     close (SOCKCLI);     return 0;}

Operation Result:

Server-side

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/87/EF/wKioL1fkkQGh0AM_ Aabobyns-bq558.png-wh_500x0-wm_3-wmp_4-s_3787741227.png "title=" Qq20160923101830.png "alt=" wKioL1fkkQGh0AM_ Aabobyns-bq558.png-wh_50 "/> customer 1

Customer 1

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/87/F2/wKiom1fkkT2B4OggAAAmPnBUoLM605.png-wh_500x0-wm_3 -wmp_4-s_306759816.png "title=" Qq20160923101936.png "alt=" Wkiom1fkkt2b4oggaaampnbuolm605.png-wh_50 "/>

Customer 2

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/87/F2/wKiom1fkkW2AjFd9AAAlrl-B8g0153.png-wh_500x0-wm_3 -wmp_4-s_2614721938.png "title=" Qq20160923102024.png "alt=" Wkiom1fkkw2ajfd9aaalrl-b8g0153.png-wh_50 "/>

4. Analysis of results

(1), utili.h in Ser.c, utili.h and CLI.C are in the same level directory;

(2), Process server: Socker is a reference counter model, close () is reduced by one, and does not really close, each time a process is created will give Socker reference counter plus 1;

(3), disadvantages: A, start and close the sub-process brings a lot of overhead; B, the system can only produce up to 512 processes, that is, up to only 512 customers, can not handle the situation of large-scale access;



This article is from the "11586096" blog, please be sure to keep this source http://11596096.blog.51cto.com/11586096/1855719

Multi-Process Network Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.