欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux下用多线程实现socket服务器和客户端的异步通信

程序员文章站 2022-03-15 21:56:54
...

前面介绍了用select函数来实现socket的异步收发数据,但是select函数也有一些缺陷,要使socket能持续地通信,select必须不停地检测,这样进程就会一直阻塞在这里,限制了功能的扩展,这里我们用多线程的方式,另创建两个线程用来发送/接收数据,即可解决这个问题,代码如下:
服务器 server.c

    #include <stdio.h>  
    #include <stdlib.h>  
    #include <string.h>  
    #include <unistd.h>  
    #include <errno.h>  
    #include <sys/types.h>  
    #include <sys/socket.h>  
    #include <netinet/in.h>  
    #include <arpa/inet.h>  
    #include <pthread.h>  

    void* recvsocket(void *ptr)  
    {  
        int fd = *(int *)ptr;  
        char str[1024];  

        while (1)  
        {  
            memset(str, 0, sizeof(str));  
            int numbytes = recv(fd, s, sizeof(str), 0);  
            if (numbytes <= 0)  
                break;  
            printf("%s\n", str);  
        }  
        return NULL;  
    }  

    void* sendsocket(void *ptr)  
    {  
        int fd = *(int *)ptr;  
        char str[1024];  

        while (1)  
        {  
            memset(s, 0, sizeof(str));  
            read(STDIN_FILENO, str, sizeof(str));  
            send(fd, str, strlen(str), 0);  
        }  

        return NULL;  
    }  


    int main(int arg, char *args[])  
    {  




        int port = 1234;  
        int st = socket(AF_INET, SOCK_STREAM, 0); 



    int opt = SO_REUSEADDR;
    setsockopt(st, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

        struct sockaddr_in addr; 
        memset(&addr, 0, sizeof(addr));  
        addr.sin_family = AF_INET; 
        addr.sin_port = htons(port);  
        addr.sin_addr.s_addr = htonl(INADDR_ANY); 


        if (bind(st, (struct sockaddr *) &addr, sizeof(addr)) == -1)  
        {  
            printf("bind failed %s\n", strerror(errno));  
            return EXIT_FAILURE;  
        }  


        if (listen(st, 20) == -1)  
        {  
            printf("listen failed %s\n", strerror(errno));  
            return EXIT_FAILURE;  
        }  
        printf("listen success\n");  




        int client_st = 0;  
        struct sockaddr_in client_addr;

        pthread_t thrd1, thrd2;  
        while (1)  
        {  
            memset(&client_addr, 0, sizeof(client_addr));  
            socklen_t len = sizeof(client_addr);  
        printf("waiting for client.......\n");
            client_st = accept(st, (struct sockaddr*) &client_addr, &len);  
            if (client_st == -1)  
            {  
                printf("accept failed %s\n", strerror(errno));  
                return EXIT_FAILURE;  
            }  
            printf("accept by %s\n", inet_ntoa(client_addr.sin_addr));  
            pthread_create(&thrd1, NULL, recvsocket, &client_st);  
            pthread_create(&thrd2, NULL, sendsocket, &client_st);  
        }  
        close(st);  
        return 0;  
    }  

客户端 client.c

    #include <stdio.h>  
    #include <stdlib.h>  
    #include <string.h>  
    #include <unistd.h>  
    #include <errno.h>  
    #include <sys/types.h>  
    #include <sys/socket.h>  
    #include <netinet/in.h>  
    #include <arpa/inet.h>  
    #include <pthread.h>  

    void* recvsocket(void *ptr)  
    {  
        int fd = *(int *)ptr;  
        char str[1024];  

        while (1)  
        {  
            memset(str, 0, sizeof(str));  
            int numbytes = recv(fd, s, sizeof(str), 0);  
            if (numbytes <= 0)  
                break;  
            printf("%s\n", str);  
        }  
        return NULL;  
    }  

    void* sendsocket(void *ptr)  
    {  
        int fd = *(int *)ptr;  
        char str[1024];  

        while (1)  
        {  
            memset(s, 0, sizeof(str));  
            read(STDIN_FILENO, str, sizeof(str));  
            send(fd, str, strlen(str), 0);  
        }  

        return NULL;  
    }  
    int main(int arg, char *args[])  
    {  




        int port = 1234;  
        int st = socket(AF_INET, SOCK_STREAM, 0); 



    int opt = SO_REUSEADDR;
    setsockopt(st, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

        struct sockaddr_in addr; 
        memset(&addr, 0, sizeof(addr));  
        addr.sin_family = AF_INET; 
        addr.sin_port = htons(port);  
        addr.sin_addr.s_addr = htonl(INADDR_ANY); 


        if (bind(st, (struct sockaddr *) &addr, sizeof(addr)) == -1)  
        {  
            printf("bind failed %s\n", strerror(errno));  
            return EXIT_FAILURE;  
        }  


        if (listen(st, 20) == -1)  
        {  
            printf("listen failed %s\n", strerror(errno));  
            return EXIT_FAILURE;  
        }  
        printf("listen success\n");  




        int client_st = 0;  
        struct sockaddr_in client_addr;

        pthread_t thrd1, thrd2;  
        while (1)  
        {  
            memset(&client_addr, 0, sizeof(client_addr));  
            socklen_t len = sizeof(client_addr);  
        printf("waiting for client.......\n");
            client_st = accept(st, (struct sockaddr*) &client_addr, &len);  
            if (client_st == -1)  
            {  
                printf("accept failed %s\n", strerror(errno));  
                return EXIT_FAILURE;  
            }  
            printf("accept by %s\n", inet_ntoa(client_addr.sin_addr));  
            pthread_create(&thrd1, NULL, recvsocket, &client_st);  
            pthread_create(&thrd2, NULL, sendsocket, &client_st);  
        }  
        close(st);  
        return 0;  
    }        

创建两个线程并发、并行地工作,分别进行发送/接收数据,(其实仅用创建一个线程,在主线程也可以完成相应的发送和接收),但是这样也有一个缺点,那就是如果要在异步通信程序上扩展其他的功能,那么接收数据的工作最好全部由接收数据的线程来完成,因为如果在其他线程中加入recv语句接收数据,那么线程之间会争抢资源,这样就无法判断是哪一个线程接收到了数据。