Possible causes of gethostbyname () failure!

Source: Internet
Author: User

From: http://blog.163.com/lyzaily@126/blog/static/4243883720091025102830596/

Gethostbyname () can be used to convert the domain name of a server into the IP address of the server. Note that the domain name of a server can correspond to multiple different IP addresses, that is to say, when you access the google server through the google domain name, you may access different google computers at different times. Why do I use a domain name instead of an IP address? In my other blog post, how do I convert a domain name address to a corresponding IP address? Description.

Before explaining one of the reasons why the function fails to be executed, we need to know the parameters and return values of the function.

Function prototype:

Struct hostent FAR * gethostbyname (const char FAR *Name);

Name: A pointer pointing to the host name string to be parsed and ending with NULL.

Returned value: A pointer pointing to a struct hostent type pointer.

 

Note the following when using this function:

(1) name pointer to the string, must be a host name string; for example: we want to log on to the google homepage, this needs to input http://www.google.com/in the browser /, the so-called host name string means to remove the remaining part of the "http: //" Header "" www.google.com ". If you use" http://www.google.com/#", the execution of the region will not get a real result.

(2) Let's take a look at the struct hostent.

Struct hostent {

Char FAR *H_name;

Char FAR *H_aliases;

ShortH_addrtype;

ShortH_length;

Char far *H_addr_list;

};

For more information about the struct, see MSDN.H_addr_listMember. This member is a pointer type variable pointing to the pointer, that is, a pointer pointing to the pointer array. Each member of the array points to a vertex IP address string, all these IP addresses correspond to the domain name to be converted. What are the IP addresses corresponding to this domain name? The pointer array ends with NULL. We can use this flag to traverse the pointer array to obtain all IP address strings corresponding to the domain name.

 

The following code shows how to use this function to obtain the IP address. This code was written by someone else. I just modified it a little and used it for testing to explain my conclusion above, I have passed the test on VS2005. You can test the program on your own. Also, pay attention to how the red mark of the Code traverses the IP Address:

# Include <winsock2.h>
# Include <stdio. h>
# Include <stdlib. h>

# Define name "www.baidu.com"
Void main ()
{

WSADATA wsadata;
Int n;
Char hostname [256];
Hostent * phostent;
Protoent * pprotoent;
Struct sockaddr_in sa;
If (WSAStartup (MAKEWORD (1,1), & wsadata )! = 0)
{
Printf ("wsastartup () failed !! \ N ");
Return;
}
// Printf ("-------------------------- \ n ");
// If (gethostname (hostname, sizeof (hostname ))! = 0)
//{
// Printf ("gethostbyname () error !! \ N ");
// Return;
//}
Memset (hostname, 0, sizeof (hostname ));
Sprintf (hostname, "% s", name );
Printf ("local host name: % s \ n", hostname );
Printf ("------------------ \ n ");
Phostent = gethostbyname (hostname );
If (phostent = NULL)
{
Printf ("gethostbyname () error !! \ N ");
Return;
}
Printf ("name: % s \ nalianses: % s \ naddrtype: % d \ nlength: % d \ n", phostent-> h_name, phostent-> h_aliases, phostent-> h_addrtype, phostent-> h_length );
For (n = 0; phostent-> h_addr_list [n]; n ++)
{
Memcpy (& sa. sin_addr.s_addr, phostent-> h_addr_list [n], phostent-> h_length );
Printf ("address: % s \ n", inet_ntoa (sa. sin_addr ));
}
Printf ("-------------------- \ n ");
Pprotoent = getprotobyname ("tcp ");
If (pprotoent = NULL)
{
Printf ("getprotobyname () failed !! \ N ");
Return;
}
Printf ("name: % \ nproto: % d \ n", pprotoent-> p_name, pprotoent-> p_proto );
For (n = 0; pprotoent-> p_aliases [n]; n ++)
{
Printf ("aliases: % s \ n", pprotoent-> p_aliases [n]);
}
WSACleanup ();

}

 

[Knowledge supplement]

Before we understand HTTP, it is necessary to clarify the composition of the URL, for example:

Http://www.microsoft.com/china/index.htm. Its meaning is as follows:

1. http: //: Indicates Hypertext Transfer Protocol. A web page is displayed on the microsoft.com server, which is usually not input;

2. www: represents a Web (World Wide Web) server;

3.microsoft.com/:the Domain Name of the server with web pages or the name of the site server;

4. China/: The subdirectory on the server, just like our folder;

5.Index.htm: index.htm is an HTML file (webpage) in the folder ).

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.