linux網路編程之socket(七):一個進程發起多個串連和gethostbyname等函數

來源:互聯網
上載者:User

一、在前面講過的最簡單的回射客戶/伺服器程式中,一個用戶端即一個進程,只會發起一個串連,只要稍微修改一下就可以讓一個用戶端發起多個串連,然後只利用其中一個串連發送資料。

先來認識一個函數getsockname

  #include <sys/socket.h>
  int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

利用此函數可以得到某串連sockfd的地址資訊,如ip地址和連接埠,這可以協助我們判斷髮起了多少個串連。

我們假設一個用戶端發起了5個串連,如:


此時根據以前說過的fork程式,伺服器端會產生5個子進程對其進行服務。

修改過後的用戶端程式如下:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*************************************************************************
    > File Name: echoser.c
    > Author: Simba
    > Mail: dameng34@163.com
    > Created Time: Fri 01 Mar 2013 06:15:27 PM CST
 ************************************************************************/

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

#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while (0)

void do_echocli(int sock)
{

    char sendbuf[1024] = {0};
    char recvbuf[1024] = {0};

    while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
    {

        writen(sock, sendbuf, strlen(sendbuf));

        int ret = readline(sock, recvbuf, sizeof(recvbuf)); //按行讀取
        if (ret == -1)
            ERR_EXIT("read error");
        else if (ret  == 0)   //伺服器關閉
        {
            printf("server close\n");
            break;
        }

        fputs(recvbuf, stdout);

        memset(sendbuf, 0, sizeof(sendbuf));
        memset(recvbuf, 0, sizeof(recvbuf));

    }

    close(sock);
}

int main(void)
{
    int sock[5];
    int i;
    for (i = 0; i < 5; i++)
    {
        if ((sock[i] = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
            //  listenfd = socket(AF_INET, SOCK_STREAM, 0)
            ERR_EXIT("socket error");

        struct sockaddr_in servaddr;
        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(5188);
        servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        /* inet_aton("127.0.0.1", &servaddr.sin_addr); */

        if (connect(sock[i], (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
            ERR_EXIT("connect error");

        struct sockaddr_in localaddr;
        socklen_t addrlen = sizeof(localaddr);
        if (getsockname(sock[i], (struct sockaddr *)&localaddr, &addrlen) < 0)
            ERR_EXIT("getsockname error");
        /* getpeername()擷取對等方的地址 */
        printf("local ip=%s port=%d\n", inet_ntoa(localaddr.sin_addr),
               ntohs(localaddr.sin_port));
    }
    /* 一個進程也可以發起多個socket串連,因為每次的連接埠號碼都不同 */
    do_echocli(sock[0]); //發起5個通訊端串連,但只藉助第一個套介面通訊

    return 0;
}

在上述程式中,我們發起5個sock串連,但只是使用sock0通訊,且利用getsockname 列印5個串連的資訊。

先運行伺服器程式,再運行用戶端,輸出如下:

simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ ./echocli_5sock 
local ip=127.0.0.1 port=53094
local ip=127.0.0.1 port=53095
local ip=127.0.0.1 port=53096
local ip=127.0.0.1 port=53097
local ip=127.0.0.1 port=53098
ferwgeht
ferwgeht


即每個串連的ip地址是一樣的,但連接埠號碼不同,伺服器方面通過accept返回的資訊也列印出串連資訊,如下:

simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ ./echoser_recv_peek 
recv connect ip=127.0.0.1 port=53094
recv connect ip=127.0.0.1 port=53095
recv connect ip=127.0.0.1 port=53096
recv connect ip=127.0.0.1 port=53097
recv connect ip=127.0.0.1 port=53098
ferwgeht


由於是多個串連,當用戶端關閉而導致伺服器子進程read 返回0退出進程時,很可能會產生殭屍進程,如:


最簡單的辦法就是父進程直接忽略SIGCHLD訊號,即signal(SIGCHLD, SIG_IGN);

如果我們想要捕獲SIGCHLD訊號的話,在訊號處理函數中不能只調用一次wait/waitpid 函數,因為用戶端退出發出FIN段的時機是不一定的,如果都能按一定時間順序發送給5個伺服器子進程,即子進程發生SIGCHLD訊號給父進程的時間有前後之分,那handler函數會被調用多次,則是允許的,也不會產生殭屍進程;但當SIGCHLD訊號同時到達,因為不可靠訊號不能排隊導致訊號只儲存一個,即其餘訊號會丟失,則產生的殭屍進程個數是不確定的,因為按前面所說取決於5個SIGCHLD訊號到達的次序。解決的辦法很簡單,只要在handler函數中while
迴圈一下就ok 了,只要接收到一個SIGCHLD訊號,則5個子進程都會被清理掉,如下所示:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
signal(SIGCHLD, handler);
.....................

void handler(int sig)
{
    /*  wait(NULL); //只能等待第一個退出的子進程 */
    /* 即使因為幾個串連同時斷開,訊號因不能排隊而父進程只收到一個訊號
     * 直到已經waitpid到所有子進程,返回0,才退出迴圈 */
    while (waitpid(-1, NULL, WNOHANG) > 0)
        ;
}


實際上使用 while (wait(NULL) > 0) ; 也可以達到同樣的效果。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

二、與前面說的getsockname 類似的函數還有getpeername、gethostname、gethostbyname、gethostbyaddr 等,現在著重來看一下gethostname 和 gethostbyname 的使用。

 #include <unistd.h>
   int gethostname(char *name, size_t len);

 #include <netdb.h>
struct hostent *gethostbyname(const char *name);

gethostname 可以得到主機名稱,而gethostbyname 可以通過主機名稱得到一個結構體指標,可以通過此結構體得到與主機相關的ip地址資訊等。

       The hostent structure is defined in <netdb.h> as follows:

           struct hostent {
               char  *h_name;            /* official name of host */
               char **h_aliases;         /* alias list */
               int    h_addrtype;        /* host address type */
               int    h_length;          /* length of address */
               char **h_addr_list;       /* list of addresses */
           }
           #define h_addr h_addr_list[0] /* for backward compatibility */


下面寫個小程式測試一下:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
#include<netdb.h>

#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while (0)

int getlocalip(char *ip)
{
    char host[100] = {0};
    if (gethostname(host, sizeof(host)) < 0)
        return -1;

    struct hostent *hp;
    if ((hp = gethostbyname(host)) == NULL)
        return -1;
    //  #define h_addr h_addr_list[0]
    strcpy(ip, inet_ntoa(*(struct in_addr *)hp->h_addr_list[0]));

    return 0;
}

int main(void)
{
    char host[100] = {0};
    if (gethostname(host, sizeof(host)) < 0)
        ERR_EXIT("gethostname error");

    struct hostent *hp;
    if ((hp = gethostbyname(host)) == NULL)
        ERR_EXIT("gethostbyname error");

    int i = 0;
    while (hp->h_addr_list[i] != NULL)
    {

        printf("%s\n", inet_ntoa(*(struct in_addr *)hp->h_addr_list[i]));
        i++;
    }

    char ip[16] = {0};
    getlocalip(ip);
    printf("local ip : %s\n" , ip);
    return 0;
}

輸出如下:

simba@ubuntu:~/Documents/code/linux_programming/UNP/socket$ ./getiplist 
127.0.1.1
local ip : 127.0.1.1

需要注意的是 hp->h_addr_list 是指標的指標,則hp->h_addr_list[i] 即指標,將其強制轉換為struct in_addr 類型的指標,再通過

 inet_ntoa 函數轉換成點分十進位的字串,即 此語句 inet_ntoa(*(struct in_addr *)hp->h_addr_list[i]);
 的意思。如果某主機配置了多個ip,則將輸出多個ip地址清單。


參考:

《Linux C 編程一站式學習》

《TCP/IP詳解 卷一》

《UNP》

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.