WSAStartup() detailed

Here is an explanation of this function in plain language. Just like opencv, you need to add link library functions, cv.lib, etc., and add them to additional dependencies, or through #pragma comment (lib, "cv.lib"). Only then can the header file be included to call various functions. Of course, socket programming needs to call various socket functions, but the library Ws2_32.lib and the header file Winsock2.h are required. The WSAStartup here is to explain to the operating system which library file we want to use, so that the library file is compatible with the current application Program binding, so that you can call various functions of this version of the socket.

Header file header: Winsock2.h

Library library: Ws2_32.lib
Prototype: int PASCAL FAR WSAStartup (WORD wVersionRequested, LPWSADATA lpWSAData);
Parameters: wVersionRequested is the highest version number that the caller can use provided by the Windows Sockets API . The upper byte indicates the minor version (revision) number, and the lower byte indicates the major version number.
        lpWSAData pointing WSADATA data structure of pointers for receiving Windows Sockets implementation details .

WSADATA data type: This structure is used to store the Windows Sockets data returned after the WSAStartup function is called  . It contains data executed by Winsock.dll .

  Structural prototype:  

struct WSAData {

  WORD wVersion;

  WORD wHighVersion;

  char szDescription[WSADESCRIPTION_LEN+1];

  char szSystemStatus [WSASYSSTATUS_LEN + 1];

  unsigned short iMaxSockets;

  unsigned short iMaxUdpDg;

  char *lpVendorInfo;};

  The WSADATA structure is used to save the Windows Sockets initialization information returned by the function WSAStartup.

  member:

  wVersion

  The version of the Windows Sockets specification that the Windows Sockets DLL expects the caller to use. The upper byte stores the minor version number, and the lower byte stores the major version number. You can use WORD MAKEWORD(BYTE,BYTE) to return this value, for example: MAKEWORD(1,1)

  wHighVersion

  The highest version of the Windows Sockets specification that this DLL can support. Usually it is the same as wVersion.

  szDescription

  A null-terminated ASCII string. Windows Sockets DLL copies the description of the implementation of Windows Sockets into this string, including the manufacturer's identification. The text (up to 256 characters) can contain any characters, but it should be noted that it cannot contain control characters and formatting characters. The most likely way for an application to use it is to display it (possibly truncated) in the status information.

  szSystemStatus

  A null-terminated ASCII string, Windows Sockets DLL copies the relevant status or configuration information to the string. The Windows Sockets DLL should only use this information when it is useful to the user or support staff. It should not be used as an extension of the szDescription field.

  iMaxSockets

  The maximum number of sockets that can be opened by a single process . The implementation of Windows Sockets can provide a global socket pool, which can be allocated to any process; or it can also allocate resources belonging to the process for sockets. This number can well reflect the configuration of Windows Sockets DLL or network software. Application writers can use this number to roughly indicate whether the implementation of Windows Sockets is useful for the application. For example, the X Windows server may check the value of iMaxSockets when it is started for the first time: if this value is less than 8, the application will display an error message instructing the user to reconfigure the network software (this is a possible way to use the szSystemStatus text occasion). Obviously, there is no guarantee that an application can actually allocate iMaxSockets sockets, because there may be other WindowsSockets applications in use.

  The maximum user data packet protocol (UDP) packet size that can be sent or received by the iMaxUdpDg Windows Sockets application, in bytes. If there are no restrictions on the implementation, then iMaxUdpDg is zero. In many implementations of Berkeley sockets, there is an inherent limitation for UDP packets (decomposed when necessary), the size is 8192 bytes. The implementation of Windows Sockets can restrict the allocation of fragmented reorganization buffers. For a suitable WindowsSockets implementation, the minimum value of iMaxUdpDg is 512. Note that no matter what the value of iMaxUdpDg is, it is not recommended that you send back a broadcast packet larger than the network's maximum transmission unit (MTU). (Windows Sockets API does not provide a mechanism for discovering MTU, but it will not be less than 512 bytes). It has been obsoleted in WinSock 2.0 version.

  lpVendorInfo Pointer to the data structure of the vendor. The definition of this structure (if any) is beyond the scope of the WindowsSockets specification. It has been deprecated in WinSock 2.0 version.

In fact, to put it plainly, wsastartup is mainly for the corresponding socket library binding.

WSAStartup()-function

1. WSAStartup function 
int WSAStartup (WORD wVersionRequested, LPWSADATA lpWSAData); 
The program using Socket must call the WSAStartup function before using Socket. Later, the application can call other Socket functions in the requested Socket library.
2. WSACleanup function 
int WSACleanup (void); After the 
application finishes using the requested Socket library, it must call the WSACleanup function to unbind the Socket library and release the system resources occupied by the Socket library.

 

WSAStartup()-How to use

When an application calls the WSAStartup function, the operating system searches for the corresponding Socket library according to the requested Socket version, and then binds the found Socket library to the application. Later, the application can call other Socket functions in the requested Socket library . This function returns 0 after successful execution. 
Example: If a program uses the 2.1 version of Socket, the program code is as follows: 
wVersionRequested = MAKEWORD( 2, 1 ); 
err = WSAStartup( wVersionRequested, &wsaData );

2. WSACleanup function 
int WSACleanup (void); After the 
application finishes using the requested Socket library, it must call the WSACleanup function to unbind the Socket library and release the system resources occupied by the Socket library. 

Three Socket interface database functions to retrieve Internet information about domain names , communication services and protocols , such as

gethostbyaddr、gethostbyname、gethostname、getprotolbyname

getprotolbynumber、getserverbyname、getservbyport。 
1.gethostname()

[ Function prototype] int PASCAL FAR gethostname (char FAR * name, int namelen);

[Instructions for use] This function can get the host name of the local host, where:

  name: the host name for the acquired point buffer of the pointer .

  Namelen: The size of the buffer, in bytes .

  Return value: If there is no error, return 0; otherwise, return the error code. 
2. gethostbyname()

  [Function prototype]

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

  【Instructions for use】

  This function can get the corresponding "host" from the hostname database.

  The only parameter name of this function is the host name obtained by calling the function gethostname() earlier. If there is no error, just return a pointer to the hostent structure, which can identify a list of "hosts".    

This agreement allows Windows Sockets DLL and Windows Sockets applications to jointly support a certain range of Windows Sockets versions. If the version range overlaps, the application can successfully use Windows Sockets DLL. The following diagram shows WSAStartup() in different applications How does the program and Windows Sockets DLL version work:
Application version DLL version wVersionRequested wVersion wHighVersion The final result
1.1 1.1 1.1 1.1 1.1 use 1.1
1.0 1.1 1.0 1.1 1.0 1.0 use 1.0 
1.0 1.0 1.1 1.0 1.0 1.1 use 1.0
1.1 1.0 1.1 1.1 1.1 1.1 use 1.1
1.1 1.0 1.1 1.0 1.0 failed
1.0 1.1 1.0 ——- —— WSAVERNOTSUPPORTED
1.0 1.1 1.0 1.1 1.1 1.1 1.1 use 1.1
1.1 2.0 1.1 2.0 1.1 1.1 use 1.1
2.0 1.1 2.0 1.1 1.1 Failure The

    following code snippet shows how the application that only supports Windows Sockets 1.1 version performs WSAStartup ()Called:
WORD wVersionRequested;
WSADATA wsaData;

int err;
wVersionRequested = MAKEWORD( 1, 1 );

 err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
                
               
                return;
             }

 

 

 
 
 
 if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 )

{
 
 
 WSACleanup( );
 return;

 }
 

The following code snippet illustrates how the Windows Sockets DLL that only supports version 1.1 performs WSAStartup() negotiation:
if (LOBYTE( wVersionRequested) <1 || (LOBYTE( wVersionRequested) == 1 &&
    HIBYTE( wVersionRequested) <1))

{
 return WSAVERNOTSUPPORTED;

 }

 lpWsaData->wVersion = MAKEWORD( 1, 1 );
 lpWsaData->wHighVersion = MAKEWORD( 1, 1 );
        Once the application or DLL makes a successful WSAStartup() call, it can continue to perform other required Windows Sockets API call. When it completes the service using the Windows Sockets DLL, the application or DLL must call WSACleanup() to allow the Windows Sockets DLL to release any resources of the application.
Error code:
WSASYSNOTREADY indicates the network subsystem that the network communication depends on Not yet ready.
The version of the Windows Sockets API required by WSAVERNOTSUPPORTED is not provided by a specific Windows Sockets implementation.
The Windows Sockets version indicated by the WSAEINVAL application is not supported by this DLL.
See also:
        send(), sendto(), WSACleanup() .

Under Windows, Socket is implemented in the form of DLL. A counter is maintained inside the DLL. Only the first call to WSAStartup will actually load the DLL. The subsequent calls simply increase the counter, while the function of the WSACleanup function is just the opposite. Each call decreases the counter by 1, and when the counter decreases to 0 When, the DLL is unloaded from memory! Therefore, you should call WSACleanup as many times as you call WSAStartup. 

Guess you like

Origin blog.csdn.net/wxy_csdn_world/article/details/88908411