How to obtain multiple IP addresses of a multi-point host

Source: Internet
Author: User

 

 

The multi-point host in the Network may have multiple IP addresses at the same time, especially when Dynamic Host address allocation is used, it is difficult to know what the IP address on the host is. The following uses a C program to list all IP addresses on the host. The code below is as follows:

 

Void print_all_ip (void)

 

{

 

Char szHostName [128];

 

Const char * pszAddr;

 

Struct hostent * pHost;

 

Int I, j;

 

If (gethostname (szHostName, 128) = 0)

 

{

 

PHost = gethostbyname (szHostName );

 

For (I = 0; pHost! = NULL & pHost-> h_addr_list [I]! = NULL; I ++)

 

{/* Process each IP Address */

 

PszAddr = inet_ntoa (* (struct in_addr *) pHost-> h_addr_list [I]);

 

Printf ("% s", pszAddr);/* print */

 

}

 

}

 

}

 

 

IntroductionThere are already many articles on Socket programming, so I will only briefly explain the relevant content. The gethostname function will return the information corresponding to the given host name. The definition of struct hostent in WinSock is as follows:

 

 

Struct hostent

 

{

 

Char FAR *H_name;

 

Char FAR * h_aliases;

 

ShortH_addrtype;

 

ShortH_length;

 

Char FAR * h_addr_list;

 

};

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.