1, multi-threaded network services
: Multithreaded network mode is similar to multi-process network mode; the difference is that when a new client arrives, a thread is started.
Model analysis
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/87/F3/wKiom1fkln7QEzPYAAAkKjy264I682.png-wh_500x0-wm_3 -wmp_4-s_999623383.png "title=" Qq20160923104201.png "alt=" Wkiom1fkln7qezpyaaakkjy264i682.png-wh_50 "/>
2. Code implementation
Also using integer arithmetic to simulate multithreading concurrency
(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* thread_handler (Void *arg); Void* thread_handler (Void *arg) { int sockconn = * (int *) arg; 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; &nbsP;} 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); pthread_exit (0);} 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& nbSp;len = sizeof (STRUCT&NBSP;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) { p rintf ("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)); } pthread_t tid; pthread_create (&tid, null, thread_handler, &sockconn); } 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->OP1), & (PT->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 (&OP); }else if (strcmp (cmd, "-") == 0) { op.oper = sub; inputdata (&OP); }else if (strcmp (cmd, "*") == 0) { op.oper = MUL; inputdata (&OP); }else if (strcmp (cmd, "/") == 0) { op.oper = div; inputdata (&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) &NBSP;BREAK;&NBSP;&NBSP;&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;}
Run results
Server-side
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/87/F0/wKioL1fkmzbgrLxbAABN107FgbY468.png-wh_500x0-wm_3 -wmp_4-s_1987473750.png "title=" Qq20160923110210.png "alt=" Wkiol1fkmzbgrlxbaabn107fgby468.png-wh_50 "/>
Customer 1
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/87/F3/wKiom1fkm36jhRHaAAAkSZGP3kI979.png-wh_500x0-wm_3 -wmp_4-s_829686193.png "title=" Qq20160923110321.png "alt=" Wkiom1fkm36jhrhaaaakszgp3ki979.png-wh_50 "/>
Customer 2
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/87/F3/wKiom1fkm63wXfyrAAArhiR8FnM399.png-wh_500x0-wm_3 -wmp_4-s_932182401.png "title=" Qq20160923110408.png "alt=" Wkiom1fkm63wxfyraaarhir8fnm399.png-wh_50 "/>
3. Analysis and summary
Multithreaded Network Service also has the dynamic application and release of threads, or there is a certain overhead, if there are a large number of users online, it is likely to bring the switching overhead between threads.
This article is from the "11586096" blog, please be sure to keep this source http://11596096.blog.51cto.com/11586096/1855753
Multithreaded Network Services