Linux网络编程--TCP并发服务器

一.并发服务器模型


常用的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创建子进程,在该进程中进行数据处理,然后创建了一个信号,当子进程结束后触发信号完成子进程的回收。
————————————————
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章