多進程以及多線程socket編程

在我的上篇博客中已經詳細介紹了socket編程中所需要的函數,在這裏就不過多介紹了,有需要的話可以參考上一篇博客:http://blog.csdn.net/qq_36221862/article/details/73611942

多進程,多線程socket編程與單進程不同的只是服務器端的不同,客戶端是一樣的。

多進程:父進程fork子進程, 子進程退出,但是子進程的子進程仍在運行,變成了孤兒進程,由1號進程回收,父進程不用等待子進程的子進程的退出,它倆是爺孫進程,不用回收子進程。

        pid_t id = fork();
        if(id < 0)
        { 
            close(new_sock);
        }
        else if(id == 0) //子進程
        { 
            close(listen_sock); //關閉不必要的文件描述局,不關閉的話,文教描述符表會越來越少,造成資源的浪費
            if(fork() > 0)
            { 
                exit(1);
            }
            else if(id == 0)
            { 
                //服務客戶端
            }
            else
            {
                close(new_sock);
            }
        else
        {
            close(new_sock);
        }

多進程socket編程服務器端代碼:

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

static void usage(const char* proc)  //使用說明
{ 
     printf("Usage:%s [local_ip] [local_port]\n", proc);
}

int startup(const char* _ip, int _port) //創建套接字
{ 

//  int sock = socket(AF_INET, SOCK_STREAM , 0);
//  if(sock < 0)
/// { 
//      perror("socket\n");
//      exit(2);
//  }

    **struct sockaddr_in local;
    int reuse = 0;
    struct sockaddr_in cliaddr, servaddr;
    int sock = socket(PF_INET, SOCK_STREAM,0);
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    { 
        perror("setsockopet error\n");
        return -1;                                 
    }** //避免綁定失敗,在上篇博客也已經解釋過了

    local.sin_family = AF_INET;
    local.sin_port = htons(_port);
    local.sin_addr.s_addr = inet_addr(_ip);

    if(bind(sock, (struct sockaddr*)&local, sizeof(local))<0)
    {
        perror("bind\n");
        exit(3);
    }
    if(listen(sock, 10) < 0)
    { 
        perror("listen\n");
        exit(4);
    }   
    return sock;
}

//tcp_server 127.0.0.1 8080
int main(int argc, char* argv[])
{ 
    if(argc != 3)
    { 
        usage(argv[0]);
        return 1;
    }
    int listen_sock = startup(argv[1], atoi(argv[2])); //監聽狀態
    struct sockaddr_in client;
    socklen_t len = sizeof(client);
struct sockaddr_in cliaddr, servaddr;

    while(1)
    { 
        int new_sock = accept(listen_sock, (struct sockaddr*)&client, &len);
        if(new_sock < 0)
        { 
            perror("accept\n");
            continue;  //繼續監聽,直到有新客戶到來
        }
        //獲取新客戶
        printf("get a new client,%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
        //服務新客戶
        pid_t id = fork();
        if(id < 0)
        { 
            close(new_sock);
        }
        else if(id == 0) //子進程
        { 
            close(listen_sock);
            if(fork() > 0)
            { 
                exit(1);
            }
            else if(id == 0)
            { 
                while(1)
                {       
                    char buf[1024000]; //服務器端先讀再寫
                    ssize_t s = read(new_sock, buf, sizeof(buf)-1);
                    if(s > 0) 
                    { 
                        buf[s] = 0; 
                        printf("client: %s\n", buf);
                        write(new_sock, buf, strlen(buf));
                    }
                    else if(s == 0)
                    { 
                        close(new_sock);
                        printf("client is quit...\n");
                        break;
                    }
                    else
                    { 
                        perror("read\n");       
                        close(new_sock);
                        exit(5);
                    }   
                }
            }
            close(new_sock);
        }
        else
        { 
            close(new_sock);
        }
        break;
    }
}

多線程:不用關閉多餘的文件描述符表,進程有兩張文件描述符表,而線程只有一張文件描述符表,共享進程的文件描述符表,因此不用關閉多餘的文件描述符表。

查看一個進程有多少個線程:
這裏寫圖片描述

pthread_t id;
        pthread_create(&id, NULL, handlerRequest, (void*)new_sock);  //創建線程
        pthread_detach(id); //線程分離

多線程socket編程源代碼:

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

static void usage(const char* proc)  //使用說明
{ 
     printf("Usage:%s [local_ip] [local_port]\n", proc);——
}

int startup(const char* _ip, int _port) //創建套接字
{ 

    int sock = socket(AF_INET, SOCK_STREAM , 0);
    if(sock < 0)
    { 
        perror("socket\n");
        exit(2);
    }

    struct sockaddr_in local;

    local.sin_family = AF_INET;
    local.sin_port = htons(_port);
    local.sin_addr.s_addr = inet_addr(_ip);

    if(bind(sock, (struct sockaddr*)&local, sizeof(local))<0)
    {
        perror("bind\n");
        exit(3);
    }
    if(listen(sock, 10) < 0)
    { 
        perror("listen\n");
        exit(4);
    }
    return sock;
}

void handlerRequest(void* arg)
{ 
    int new_sock = (int)arg;
    while(1)
    {           
        char buf[1024]; //服務器端先讀再寫
        ssize_t s = read(new_sock, buf, sizeof(buf)-1);
        if(s > 0) 
        { 
            buf[s] = 0;
            printf("client: %s\n", buf);
            write(new_sock, buf, strlen(buf));
        }
        else if(s == 0)
        { 
            close(new_sock);
            printf("client is quit...\n");
            break;
        }
        else
        { 
            perror("read\n");   
            close(new_sock);
            exit(5);    
        }
    }
}


//tcp_server 127.0.0.1 8080
int main(int argc, char* argv[])
{ 
    if(argc != 3)
    { 
        usage(argv[0]);
        return 1;
    }
    int listen_sock = startup(argv[1], atoi(argv[2])); //監聽狀態
    struct sockaddr_in client;
    socklen_t len = sizeof(client);

    while(1)
    { 
        int new_sock = accept(listen_sock, (struct sockaddr*)&client, &len);
        if(new_sock < 0)
        { 
            perror("accept\n");
            continue;  //繼續監聽,直到有新客戶到來
        }
        //獲取新客戶
        printf("get a new client,%s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
        //服務新客戶

        pthread_t id;
        pthread_create(&id, NULL, handlerRequest, (void*)new_sock);
        pthread_detach(id); //線程分離

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