linux下用多線程實現socket服務器和客戶端的異步通信

前面介紹了用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語句接收數據,那麼線程之間會爭搶資源,這樣就無法判斷是哪一個線程接收到了數據。

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