VC + + Network programming WSAStartup and WSACleanup functions

Source: Internet
Author: User
Tags socket name database

First, WSAStartup function

int WSAStartup
(
 WORD wVersionRequested,
 LPWSADATA lpWSAData
);

A program using the socket must call the WSAStartup function before using the socket. The first parameter of the function indicates the version of the socket that the program requests to use, where the high byte indicates the secondary version, the low byte indicates the major version, and the operating system uses the second parameter to return the version information of the requested socket. When an application calls the WSAStartup function, the operating system searches for the corresponding socket library based on the requested version of the socket, and then binds the found socket library to the application. The application can then invoke the other socket functions in the requested socket library. The function returns 0 after successful execution. Example: If a program uses the 2.1 version of the socket, then the program code is as follows:

wVersionRequested = MAKEWORD( 2, 1 );
err = WSAStartup( wVersionRequested, &wsaData );

Second, wsacleanup function int wsacleanup (void); After the application completes the use of the requested socket library, it calls the WSACleanup function to unbind the socket library and free the system resources that the socket library occupies.

Three-socket interface retrieves database functions for Internet information such as domain names, communication services, and protocols, such as

gethostbyaddr、gethostbyname、gethostname、getprotolbyname
getprotolbynumber、getserverbyname、getservbyport。

1.gethostname ()

"Function Prototypes"

int PASCAL FAR gethostname (Char FAR * name, int namelen);

"Instructions for use"

The function can get the host name of the local host, where:

name:< output > A pointer to a buffer that points to the acquired host name.

namelen:< the size of the input > buffer, in bytes.

Return value: If no error, return 0, otherwise, return the wrong generation.

2.gethostbyname ()

"Function Prototypes"

struct hostent FAR * PASCAL FAR gethostbyname (const char FAR * name);

"Instructions for use"

The function can get the corresponding "host" from the host name database.

The unique parameter name of the function is the hostname of the previous Call function GetHostName (). If there is no error, just return a batch pin pointing to the hostent structure, which identifies a "host" list.

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.