一.併發服務器模型
常用的TCP服務器模型,我們上節寫的是TCP循環服務器模型,這種模型很大的弊端就是沒辦法實現併發,在此基礎上進行優化,就出現了多線程和多進程的服務器。
他們的解決思路都是相同的,他們的數據處理都是單獨在開一個線程或者進程進行處理。
多線程和多進程的服務器模型是比較常見的,他們之間各具優勢,需要根據不同應用場景進行選擇。
二.多線程併發實例
客戶端的實現和之前的代碼相同,不做重點關注。
server.c
#include <pthread.h>
#include "net.h"
void cli_data_handle (void *arg);
int main (void)
{
int fd = -1;
struct sockaddr_in sin;
/* 1. 創建socket fd */
if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*優化4: 允許綁定地址快速重用 */
int b_reuse = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
/*2. 綁定 */
/*2.1 填充struct sockaddr_in結構體變量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT); //網絡字節序的端口號
/*優化1: 讓服務器程序能綁定在任意的IP上 */
#if 1
sin.sin_addr.s_addr = htonl (INADDR_ANY);
#else
if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
/*2.2 綁定 */
if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror ("bind");
exit (1);
}
/*3. 調用listen()把主動套接字變成被動套接字 */
if (listen (fd, BACKLOG) < 0) {
perror ("listen");
exit (1);
}
printf ("Server starting....OK!\n");
int newfd = -1;
/*4. 阻塞等待客戶端連接請求 */
/* 優化: 用多進程/多線程處理已經建立號連接的客戶端數據 */
pthread_t tid;
struct sockaddr_in cin;
socklen_t addrlen = sizeof (cin);
while (1) {
if ((newfd = accept (fd, (struct sockaddr *) &cin, &addrlen)) < 0) {
perror ("accept");
exit (1);
}
char ipv4_addr[16];
if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
perror ("inet_ntop");
exit (1);
}
printf ("Clinet(%s:%d) is connected!\n", ipv4_addr, htons (cin.sin_port));
pthread_create (&tid, NULL, (void *) cli_data_handle, (void *) &newfd);
}
close (fd);
return 0;
}
void cli_data_handle (void *arg)
{
int newfd = *(int *) arg;
printf ("handler thread: newfd =%d\n", newfd);
//..和newfd進行數據讀寫
int ret = -1;
char buf[BUFSIZ];
while (1) {
bzero (buf, BUFSIZ);
do {
ret = read (newfd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
exit (1);
}
if (!ret) { //對方已經關閉
break;
}
printf ("Receive data: %s\n", buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用戶輸入了quit字符
printf ("Client(fd=%d) is exiting!\n", newfd);
break;
}
}
close (newfd);
}
從上面的代碼中我們也可以清晰的看出,和之前的代碼整體修改的內容並不多,socket創建,綁定,監聽,都是相同的在主循環中accept在阻塞等待客戶端的連接,得到是哪個客戶端連接進來,然後打印一下信息。整個的一個連接過程就算完成了,然後創建一個線程用API函數pthread_create(),然後在線程內部進行數據的處理,處理完成後,回收連接,回收線程,整個過程完畢。
三.多進程的併發服務器
server.c
#include <pthread.h>
#include <signal.h>
#include "net.h"
void cli_data_handle (void *arg);
void sig_child_handle(int signo)
{
if(SIGCHLD == signo) {
waitpid(-1, NULL, WNOHANG);
}
}
int main (void)
{
int fd = -1;
struct sockaddr_in sin;
signal(SIGCHLD, sig_child_handle);
/* 1. 創建socket fd */
if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*優化4: 允許綁定地址快速重用 */
int b_reuse = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
/*2. 綁定 */
/*2.1 填充struct sockaddr_in結構體變量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT); //網絡字節序的端口號
/*優化1: 讓服務器程序能綁定在任意的IP上 */
#if 1
sin.sin_addr.s_addr = htonl (INADDR_ANY);
#else
if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
/*2.2 綁定 */
if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror ("bind");
exit (1);
}
/*3. 調用listen()把主動套接字變成被動套接字 */
if (listen (fd, BACKLOG) < 0) {
perror ("listen");
exit (1);
}
printf ("Server starting....OK!\n");
int newfd = -1;
/*4. 阻塞等待客戶端連接請求 */
struct sockaddr_in cin;
socklen_t addrlen = sizeof (cin);
while(1) {
pid_t pid = -1;
if ((newfd = accept (fd, (struct sockaddr *) &cin, &addrlen)) < 0) {
perror ("accept");
break;
}
/*創建一個子進程用於處理已建立連接的客戶的交互數據*/
if((pid = fork()) < 0) {
perror("fork");
break;
}
if(0 == pid) { //子進程中
close(fd);
char ipv4_addr[16];
if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
perror ("inet_ntop");
exit (1);
}
printf ("Clinet(%s:%d) is connected!\n", ipv4_addr, ntohs(cin.sin_port));
cli_data_handle(&newfd);
return 0;
} else { //實際上此處 pid >0, 父進程中
close(newfd);
}
}
close (fd);
return 0;
}
void cli_data_handle (void *arg)
{
int newfd = *(int *) arg;
printf ("Child handling process: newfd =%d\n", newfd);
//..和newfd進行數據讀寫
int ret = -1;
char buf[BUFSIZ];
while (1) {
bzero (buf, BUFSIZ);
do {
ret = read (newfd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
exit (1);
}
if (!ret) { //對方已經關閉
break;
}
printf ("Receive data: %s\n", buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用戶輸入了quit字符
printf ("Client(fd=%d) is exiting!\n", newfd);
break;
}
}
close (newfd);
}
和線程的處理是非常相似的,使用fork創建子進程,在該進程中進行數據處理,然後創建了一個信號,當子進程結束後觸發信號完成子進程的回收。
————————————————