Get IP address for domain name [gethostbyname () function] Details

Source: Internet
Author: User
Tags get ip name database
Function Description:

1. wsastartup Function
Int wsastartup
(
Word wversionrequested,
Lpwsadata
);
The program using the socket must call the wsastartup function before using the socket. The first parameter of this function indicates the socket version requested by the program. The high byte indicates the secondary version and the low byte indicates the primary version; the operating system uses the second parameter to return the request's socket version. When an application calls the wsastartup function, the operating system searches for the corresponding socket Library Based on the requested socket version, and then binds the socket Library to the application. In the future, the application can call other socket functions in the requested socket library. After the function is successfully executed, 0 is returned.
For example, if a program uses a socket of Version 2.1, the program code is as follows:
Wversionrequested = makeword (2, 1 );
Err = wsastartup (wversionrequested, & wsadata );

Ii. wsacleanup Function
Int wsacleanup (void );
After the application completes the use of the requested socket library, it needs to call the wsacleanup function to unbind from the socket library and release the system resources occupied by the socket library.
Database functions related to Internet information such as domain names, communication services, and protocols, such

Gethostbyaddr, gethostbyname, gethostname, and getprotolbyname

Getprotolbynumber, getserverbyname, and getservbyport.
1. gethostname ()

[Function prototype]

Int Pascal far gethostname (char far * Name, int namelen );

[Instructions]

This function can obtain the Host Name of the local host, where:

Name: <output> pointer to the buffer zone of the obtained host name.

Namelen: <input> buffer size, in bytes.

Return Value: If no error is returned, 0 is returned. Otherwise, is the error generation returned.
2. gethostbyname ()

[Function prototype]

Struct hostent far * Pascal far gethostbyname (const char far * Name );

[Instructions]

This function can obtain the corresponding "host" from the host name database ".

The unique parameter name of this function is the host name obtained by calling the function gethostname. If no error occurs, a batch needle pointing to the hostent structure is returned, which can identify a "host" list.

 

# Include <windows. h> # include <iostream> # include <Winsock. h> # pragma comment (Lib, "ws2_32") using namespace STD; int main () {int wsa_return; wsadata; wsa_return = wsastartup (0x0101, & wsadata ); /* structure pointer */hostent * host_entry;/* URL: www.baidu.cn */Char host_name [256] = "www.baidu.cn"; if (wsa_return = 0) {/* indicates the domain name or host name to be resolved */host_entry = gethostbyname (host_name); printf ("% s \ n", host_name); If (host_entry! = 0) {printf ("IP Address:"); printf ("% d. % d. % d. % d ", (host_entry-> h_addr_list [0] [0] & 0x00ff), (host_entry-> h_addr_list [0] [1] & 0x00ff ), (host_entry-> h_addr_list [0] [2] & 0x00ff), (host_entry-> h_addr_list [0] [3] & 0x00ff) ;}} wsacleanup ();}

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.