The following are two functions used in VC socket initialization:
1. wsastartup Function
Int wsastartup
(
Word wversionrequested,
Lpwsadata
);
Use SOCKET Program Before using socket, you must call the wsastartup function. 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 Socket 2.1 Code 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.
========================================================== ========
Gethostbyname () function
RunableSource code
It's easy, but it's hard for a person who has never used it to write a blog.
:
// Getip. cpp
# Include <windows. h>
# Include <iostream>
# Include <Winsock. h>
# Pragma comment (Lib, "ws2_32 ")
Using namespace STD;
Int main ()
{
Int wsa_return;
Wsadata;
/*************************************** ****************************
The program using the socket must call the wsastartup function before using the socket.
The first parameter of the function specifies 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,
Then bind 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.
**************************************** *************************/
Wsa_return = maid (0x0101, & wsadata );
/* Structure pointer */
Hostent * host_entry;
/* URL: http://www.google.cn /*/
Char host_name [256] = "http://www.google.cn /";
If (wsa_return = 0)
{
/* The domain name or host name to be resolved */
Host_entry = gethostbyname (host_name );
Printf ("% s \ n", host_name );
If (host_entry! = 0)
{
Printf ("Resolution IP Address :");
Printf ("% 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 ();
}