When doing network service, it is necessary to write concurrent service-side programs. 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 "style=" padding:0px; Margin:0px;vertical-align:top;border:none; "/>
the following issues need to be considered when developing services using the multithreaded model
1. Adjust the maximum file descriptor limit in the process.
2. If the thread has shared data, consider thread synchronization.
3. Service when client thread exits, exit processing (exit value, works thread is in detached state)
4. System load, as the number of linked customers increases, causing other threads to be unable to stage the CPU
.server code [ In actual development, pay special attention to function call return value judgment ]
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include < string.h> #include <ctype.h> #include <arpa/inet.h> #include <sys/socket.h># Include <sys/types.h> #include <pthread.h>/* * multi-threaded concurrent Server *author sea time 2016/06/20 */#define &NBSP;SERV_PORT&NBSP;9096//Service occupied Port #define serv_addr " 10.10.101.105 "//service takes up ip//thread handler function Void* thread_handler (Void* args) {char buf[bufsiz];//bufsiz Built-in macro 8192int n, i;int fd = (int) args;//conversion Gets the client descriptor while (1) {Bzero (buf, sizeof ( BUF) N = read (fd, buf, sizeof (BUF));//The other side has closed if (0 == n) {//close connection Close (FD);p rintf (" Close...\n "); break;} Else if (0 < n) {//Case conversion for (i = 0; i < n; i++) {buf[i] = toupper (Buf[i]);} Send to client write (fd, buf, n);}} Pthread_exit (NULL);} Int main (int argc, char* argv[]) {pthread_t tid;int listenfd, connfd;struct sockaddr_in serv_addr, clie_addr;socklen_t clie_addr_len;int opt;char str[inet_addrstrlen];//inet_addrstrlen Memory Macro 16//Create a listener socket//AF_INET:IPV4//SOCK_STREAMTCP stream Type//IPPROTO_TCPTCP protocol Listenfd = socket (af_inet, sock_stream, ipproto_tcp);//Set Port multiplexing opt = 1;setsockopt (Listenfd, sol_socket, so_reuseaddr, &opt, sizeof (opt));//initialized to 0bzero (&serv_addr, sizeof (SERV_ADDR));// Specify family: ipv4serv_addr.sin_family = af_inet;//Specify the port number and convert to network byte order serv_addr.sin_port = htons ( Serv_port);//Specify IP and convert to network byte order Inet_pton (AF_INET,&NBSP;SERV_ADDR,&NBSP;&SERV_ADDR.SIN_ADDR.S_ADDR);// Bind to a listening socket bind (listenfd, (struct sockaddr*) &serv_addr, sizeof (SERV_ADDR));//Set simultaneous connection request upper limit listen ( Listenfd, somaxconn); while (1) {clie_addr_len = sizeof (CLIE_ADDR);//block Get connection connfd = Accept (listenfd, (struct (sockaddr*) &clie_addr, &clie_addr_len);//output Connected client information printf ("%s:%d connect successfully!\ N ", inet_ntop (af_inet, &clie_addr.sin_addr.s_addr, str, sizeof (str)), ntohs (clie_ Addr.sin_port));//Create a thread with which to process, and assign the descriptor to the Works thread Pthread_create (&tid, NULL, thread_handler, (void*) CONNFD);//Set Thread detach Pthread_detach (TID);} Close (LISTENFD); return 0;}
multi-threaded and multi- process by Model CPU debugging in Linux, so the use of multi-threaded and multi-process is very convenient in the case of a few connections.
This article is from the "Sea" blog, be sure to keep this source http://lisea.blog.51cto.com/5491873/1791023
Linux Network programming-----> High Concurrency---> multi-threaded concurrent servers