Linux Network Programming gethostbyname ()

Source: Internet
Author: User
Tags aliases

gethostbyname () returns a pointer to the hostent structure that contains the host name and address information for the given host name. The declaration of the structure is consistent with the GETHOSTADDR (). Returns the host information corresponding to the given host name. #include<winsock2.h>structHostent far *pascal far gethostbyname (Const CharFAR * name); Name: A pointer to the host name. Linux version #include <netdb.h>structHostent *gethostbyname (Const Char*hostname); Return: Non-null pointer--Success, NULL pointer--error, set H_errno comment gethostbyname () returns a hostent structure pointer to the host name and address information corresponding to the given host name. The declaration of the structure is consistent with the GETHOSTADDR (). The returned pointer points to a structure that is assigned by the Windows Sockets implementation. The application should not attempt to modify the structure or release any part of it. In addition, there is only one copy of this structure per thread, so applications should copy the information they need before issuing other Windows Scokets API calls. The gethostbyname () implementation does not need to recognize the IP address string that is passed to it. For such a request, the IP address string should be treated as an unknown hostname. If an application has an IP address string to process, it should use the inet_addr () function to convert the address string to an IP address and then call GETHOSTBYADDR () to get the hostent structure. return value if no error occurs, gethostbyname () returns a pointer to the hostent structure as described above, otherwise returns a null pointer. The application can get a specific error code by WSAGetLastError (). Error code wsanotinitialised must successfully call WSAStartup () before applying this API. Wsaentdown the Windows sockets implementation detected a network subsystem error. Wsahost_not_found did not find an authorized answer host. Wsatry_again did not find an unauthorized host, or Serverfail. Wsano_recovery unrecoverable error, formerr,refused,notimp. Wsano_data a valid name, but there is no data record about the request type. Wsaeinprogress a blocked Windows Sockets operation is in progress. The Wsaeintr blocking call was canceled by WSACancelBlockingCall (). It is important to note that the gethostbyname () function belongs to the Winsock API Library, and the WSA must be called before using the Winsock API-startup function, only if the function returns successfully (indicating that the application has successfully established a connection with the Winsock library), the application can invoke functions in other Windows Sockets DLLs. When the program is about to end, you must also call the WSACleanup function to clean up so that it frees up the resources it occupies. The WSACleanup function is used to end the use of Windows Sockets DLLs. See also: WSAAsyncGetHostByName (), gethostbyaddr () ############################################################## http: //hi.baidu.com/zengzhaonong/blog/item/162213f4c18d6cec7609d7c1.htmlgethostbyname ()--Obtain an IP address by domain name or hostname #include<netdb.h>#include<sys/socket.h>structHostent *gethostbyname (Const Char*name); The passed-in value of this function is the domain name or hostname, for example"www.google.cn"and so on. The outgoing value, which is a hostent structure.    If the function call fails, NULL is returned. structhostent {Char*h_name;/*official domain name of host*/                  Char**h_aliases;/*null-terminated array of domain names*/        intH_addrtype;/*host address Type (AF_INET)*/        intH_length;/*length of an address, in bytes*/        Char**h_addr_list;/*null-terminated Array of in_addr structs*/        #defineH_ADDR H_addr_list[0]    }; Hostent-H_name represents the canonical name of the host.    For example Www.google.com's canonical name is actually www.l.google.com. Hostent-H_aliases represents the alias of the host. Www.google.com is Google's own nickname.    Sometimes, some hosts may have several aliases, these, in fact, are for easy user memory and for their own site to take more names. Hostent-H_addrtype represents the type of host IP address, whether it is IPv4 (af_inet) or Pv6 (AF_INET6) hostent-H_length represents the length of the host IP address hostent-H_ADDR_LISST represents the IP address of the host, note that this is stored in the network byte order. Never use printf directly .%s parameter to hit this thing, there will be problems in Wow.    So to really need to print out this IP, you need to call Inet_ntop (). Const Char*inet_ntop (intAfConst void*SRC,Char*DST, socklen_t cnt): This function, is the network address structure of type AF src, converted to a host sequence of string form, stored in the length of the CNT string. Returns a pointer to the DST point. If the function is called incorrectly, the return value is null. #include<netdb.h>#include<sys/socket.h>#include<stdio.h>intMainintargcChar**argv) {    Char*PTR, * *pptr; structHostent *hptr; Charstr[ +];/*the first parameter after the command, which is the domain name or hostname to resolve*/ptr= argv[1]; /*call gethostbyname (). The call results are present in Hptr*/    if((hptr = gethostbyname (ptr)) = =NULL) {printf ("gethostbyname error for host:%s\n", PTR); return 0; } /*call out the canonical name of the host*/printf ("official hostname:%s\n",hptr->h_name);/*The host may have multiple aliases, and all aliases are typed separately*/     for(pptr = hptr->h_aliases; *pptr! = NULL; pptr++) printf ("alias:%s\n",*pptr); /*according to the address type, the address is typed out*/    Switch(hptr->H_addrtype) {         Caseaf_inet: Caseaf_inet6:pptr=hptr->h_addr_list;/*call out all the addresses you just got. where the Inet_ntop () function is called*/             for(; *pptr!=null; pptr++) printf ("address:%s\n", Inet_ntop (hptr->h_addrtype, *pptr, str,sizeof(str))); printf ("First address:%s\n", Inet_ntop (hptr->h_addrtype, hptr->h_addr, str,sizeof(str)));  Break; default: printf ("Unknown address type\n");  Break; }    return 0;} Compile Run-----------------------------# gcc test.c#./A. outwww.baidu.comofficial hostname:www.a.shifen.comalias:www.baidu.comaddress:121.14.88.11Address:121.14.89.11First Address:121.14.88.11 

Linux Network Programming gethostbyname ()

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.