Linux Network programming-----> High Concurrency---> multi-process concurrent servers

Source: Internet
Author: User
Tags close close server port htons

It is necessary to write concurrent service-side programs while doing network service. The stability of the front-end client application is partly dependent on the client itself, and more depends on whether the server is fast enough and stable enough.

The common Linux concurrent Server model;

        • Multi-Process Concurrent server

        • Multi-threaded Concurrent server

        • Select multi-channel I/O transfer server

        • Poll Multi-channel I/O forwarding server

        • Epool Multi-Channel I/O forwarding server.


This session mainly discusses the multi-threaded concurrent Server model:

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/82/EC/wKioL1dlV3ORUbUGAAISrviO-QM731.png-wh_500x0-wm_3 -wmp_4-s_3902690494.png "title=" concurrency model. png "alt=" wkiol1dlv3orubugaaisrvio-qm731.png-wh_50 "/>


consider the following points when using multiple-process concurrent servers:

        • Parent Process Maximum file description (the parent process needs close close Accpet The new file descriptor returned)

        • Number of processes created in the system (related to memory size)

        • Excessive process creation reduces overall service performance (process scheduling)


    .server code [pay special attention to function call return value in actual development]

#include  <stdio.h> #include  <stdlib.h> #include  <unistd.h> #include  <sys /types.h> #include  <sys/socket.h> #include  <arpa/inet.h> #include  <string.h># include <signal.h> #include  <sys/wait.h> #include  <ctype.h>/* * multi-process Concurrent server  *author: sea time 2016/06/18 */#define &NBSP;SERV_PORT&NBSP;9096//Server port number #define  serv_addr  "10.10.101.102"//server IP address void wait_process (int num) {//Reclaim sub-process while (0 <  Waitpid (0, null, wnohang)) {;}} Int main (int argc, char* argv[]) {pid_t pid;//creation process uses int lfd;//listener socket int cfd;/ /Client Socket struct sockaddr_in serv_addr;//server address family struct,  binding using struct sockaddr_in clie_addr;// The client address family structure, the receive connection uses the socklen_t clie_addr_len;//client address family size, the receive connection uses the charbuf[bufsiz];int n, i;//registration process to end the signal, Recycle end of subprocess signal (sigchld, wait_process);//Create Listener Socket//af_inet: Specify Ipv4//sock_stream: Specify tcp//0: The function will automatically specify the protocol Lfd = socket (AF_INET,&NBSP;SOCK_STREAM,&NBSP;0) According to the specified type, and/or all is initialized to 0bzero (&AMP;SERV_ADDR,  sizeof (SERV_ADDR));//Specify family: ipv4serv_addr.sin_family = af_inet;//Specify IP address and convert to network byte order Inet_pton (AF_INET ,  serv_addr, &serv_addr.sin_addr.s_addr);//Specify the port number and convert it to network byte order serv_addr.sin_port = htons ( Serv_port);//bind to the listener socket bind (lfd,  (struct sockaddr*) &serv_addr, sizeof (SERV_ADDR));// Specify a listening socket and set simultaneous connection upper limit//somaxconn:  system defaults to 128listen (lfd, somaxconn);//Loop Get connection while (1) {clie_addr_len =  sizeof (CLIE_ADDR);//Block Get Connection cfd = accept (lfd,  (struct sockaddr*) &clie_addr,  &clie_addr_len)///Play screen prompt to connect successfully printf ("%s:%d connected successfully!\n",  inet_ntop (Af_inet,  &clie_addr.sin_addr.s_addr, buf, sizeof (BUF)),  ntohs (Clie_addr.sin_port));//create Process PID  = fork ();//Sub-process if (0 == pid) {//child process close listener socket close (LFD); The parent process closes the connection socket close (CFD); Child processes communicate with client processes while (0&NBSP;==&NBSP;PID) {BzeRo (buf, sizeof (BUF));//Read messages sent by the client N = read (cfd, buf, sizeof (BUF));//Client off if (0  == n) {Close (CFD);p rintf ("%s:%d disconnect successfully!\n",  inet_ntop (AF_INET,  &clie_addr.sin_addr.s_addr, buf, sizeof (BUF)),  ntohs (Clie_addr.sin_port)); exit (1);} Convert to size for (i = 0; i < n; i++) {buf[i] = toupper (buf[i]);} Write back to the client (cfd, buf, n);} return 0;}

. Client

#include  <stdio.h> #include  <stdlib.h> #include  <arpa/inet.h> #include  < sys/types.h> #include  <sys/socket.h> #include  <string.h> #include  <unistd.h>/ * * socket Client Connection Server  */#define &NBSP;SERV_PORT&NBSP;9096//server port#define serv_addr  " 10.10.101.102 "//Server Ipint main (int argc, char* agrv[]) {int sfd;struct sockaddr_in  serv_addr;char buf[bufsiz];int n;//Create a network socket//af_inet: Specify Ipv4//sock_stream: Specify tcp// 0: The function will automatically specify the protocol Sfd = socket (AF_INET,&NBSP;SOCK_STREAM,&NBSP;0) According to the specified type;//Initialize Bzero (&serv_addr,  sizeof (SERV_ADDR));//Specify the family serv_addr.sin_family = af_inet;//specify the IP address and convert to network byte order Inet_pton (Af_inet, serv _ADDR,&NBSP;&AMP;SERV_ADDR.SIN_ADDR.S_ADDR);//Specify port and convert to network byte order serv_addr.sin_port = htons (SERV_PORT);// Connect to server Connect (sfd,  (struct sockaddr*) &serv_addr, sizeof (SERV_ADDR));//Read keyboard input while (Fgets (BUF,  sizeof (BUF),  stdin)  ! = null) {//write server send data write (Sfd, buf, strlen (BUF)); Bzero (Buf, sizeof (BUF));//read server send data n =  read (sfd, buf, sizeof (BUF));//server off if (0 == n) {close (SFD); break;} Output write (stdout_fileno, buf, n) to the screen, bzero (buf, sizeof (BUF));}}


This article is from the "Sea" blog, be sure to keep this source http://lisea.blog.51cto.com/5491873/1790697

Linux Network programming-----> High Concurrency---> multi-process concurrent servers

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.