socket programming twenty-four: gethostbyname () function: to obtain an IP address through the domain name

The client directly using the IP address will be a serious shortcoming, once the IP address change (IP addresses change frequently), client software error occurs.

The domain name will be a lot easier to use, as long as a year after the domain name registration renewals will never own, modify DNS to replace the IP address, will not affect the normal use of the software.

About domain name registration, domain name resolution, host file, DNS server, etc. This section does not explain in detail, the reader make up their own brain. This section focuses on explaining how to use the domain name.

Obtain an IP address through the domain name

Domain name is just a mnemonic IP address, the purpose is easy to remember and can not find the target computer through the domain name, you must want to convert domain names into IP addresses before communication.

gethostbyname () function can accomplish this conversion, it is a prototype:


struct hostent *gethostbyname(const char *hostname);

hostname is the host name, which is the domain name. When using this function, as long as the domain name string is transmitted, it will return the corresponding IP address. Return address information is loaded hostent structure, the structure is defined as follows:


struct hostent{
char *h_name; //official name
char **h_aliases; //alias list
int h_addrtype; //host address type
int h_length; //address lenght
char **h_addr_list; //address list
}

As can be seen from the structure, not only return the IP address, also comes with additional information, you readers can focus on the last member h_addr_list. The following is a description of each member:

  • h_name: official domain name (Official domain name). Official domain name on behalf of a home, but in fact some famous company's domain name was not registered with the official domain name.
  • h_aliases: Aliases can be accessed through the same host multiple domain names. The same IP address can bind multiple domain names, so in addition to the current domain name can also specify a different domain name.
  • h_addrtype: gethostbyname () only supports IPv4, also supports IPv6, you can obtain the IP address family addresses (address type) information via this member, IPv4 correspond AF_INET, IPv6 correspond AF_INET6.
  • h_length: save the IP address length. A length of 4 bytes for IPv4, IPv6, a length of 16 bytes.
  • h_addr_list: This is the most important members. Save the corresponding IP address of an integer by the members. For more server users may assign multiple IP addresses to the same domain name, the use of multiple servers to balance the load.


hostent structure variable composition shown below:

Composition hostent structure


The following code main presentation gethostbyname () is applied, and the characteristics described hostent structure:


#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")

int main(){
WSADATA wsaData;
WSAStartup( MAKEWORD(2, 2), &wsaData);

struct hostent *host = gethostbyname("www.baidu.com");
if(!host){
puts("Get IP address error!");
system("pause");
exit(0);
}

//别名
for(int i=0; host->h_aliases[i]; i++){
printf("Aliases %d: %s\n", i+1, host->h_aliases[i]);
}

//地址类型
printf("Address type: %s\n", (host->h_addrtype==AF_INET) ? "AF_INET": "AF_INET6");

//IP地址
for(int i=0; host->h_addr_list[i]; i++){
printf("IP addr %d: %s\n", i+1, inet_ntoa( *(struct in_addr*)host->h_addr_list[i] ) );
}

system("pause");
return 0;
}

Operating results:
Aliases 1: www.baidu.com
Address of the type: AF_INET
IP addr 1: 61.135.169.121
IP addr 2: 61.135.169.125

Published 33 original articles · won praise 30 · views 20000 +

Guess you like

Origin blog.csdn.net/baidu_15547923/article/details/90230468
Recommended