WSAStartup () function to load the DLL and

This section explains under Windows DLL loading learn  Linux  the Socket readers can skip.

WinSock (Windows Socket) programming dependent dynamic link library in the system provides (DLL), there are two versions:

  • DLL is earlier wsock32.dll, a size of 28KB, the corresponding header file winsock1.h;
  • The latest DLL is ws2_32.dll, size is 69KB, the corresponding header file winsock2.h.


Almost all Windows operating systems already support ws2_32.dll, including personal operating system Windows 95 OSR2, Windows 98, Windows Me, Windows 2000, XP, Vista, Win7, Win8, Win10 and server operating systems Windows NT 4.0 SP4, Windows Server 2003, Windows Server 2008 and so on, so you can not hesitate to use the latest ws2_32.dll.

Before using DLL DLL must be loaded into the current program, you can load at compile time, you can also load the program is running, we have been in " dynamic-link library DLL is loaded: implicitly loaded (load loading) and explicitly loaded (load runtime) "were explained.

As used herein #pragmacommand to load at compile time:

#pragma comment (lib, "ws2_32.lib")

WSAStartup () function

Before using the DLL, also need to call WSAStartup () function initializes to the version specification indicates WinSock, its prototype is:

int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);

wVersionRequested version number for the WinSock specification, the low byte is the major version number, high byte minor version number (Revision Number); lpWSAData WSAData a pointer to the structure.

About WinSock specification

The latest version of WinSock 2.2 specification, earlier there 2.1,2.0,1.1,1.0, ws2_32.dll supports all standard, while wsock32.dll only supports 1.0 and 1.1.

wsock32.dll has been able to support the development of TCP / IP communication program, ws2_32.dll mainly adds support for other protocols, but recommend using the latest 2.2 version.

wVersionRequested parameter is used to indicate the version we want to use, which is of type WORD, equivalent to unsigned short, is an integer, it is necessary () macro function to the version number conversion MAKEWORD. E.g:

MAKEWORD (1, 2); // a major version, minor version number is 2, return 0x0201 
MAKEWORD (2, 2); // 2, major version, minor version number is 2, return 0x0202

About WSAData structure

WSAStartup () function is executed after successful, it sends information relating to the structure variable WSAData ws2_32.dll written. WSAData defined as follows:

 
  1. typedef struct WSAData {
  2. WORD wVersion; //ws2_32.dll recommended that we use the version number
  3. WORD wHighVersion; //ws2_32.dll support the highest version number
  4. // a string ending with null, used to illustrate the realization ws2_32.dll and vendor information
  5. char szDescription[WSADESCRIPTION_LEN+1];
  6. // string ending with a null, for explaining the configuration and status ws2_32.dll
  7. char szSystemStatus [WSASYS_STATUS_LEN + 1];
  8. unsigned short iMaxSockets; no longer used after //2.0
  9. unsigned short iMaxUdpDg; no longer used after //2.0
  10. char FAR * lpVendorInfo; no longer used after //2.0
  11. } WSADATA, * LPWSADATA;

The last three members have been abandoned, and szSystemStatus szDescription information contained basically no practical value, two members of the audience can focus on just before. Consider the following code:

 
  1. #include <stdio.h>
  2. #include <winsock2.h>
  3. #pragma comment (lib, "ws2_32.lib")
  4.  
  5. int main () {
  6. WSAData wsaData;
  7. WSAStartup( MAKEWORD(2, 2), &wsaData);
  8.  
  9. printf("wVersion: %d.%d\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
  10. printf("wHighVersion: %d.%d\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion));
  11. printf("szDescription: %s\n", wsaData.szDescription);
  12. printf("szSystemStatus: %s\n", wsaData.szSystemStatus);
  13.  
  14. return 0;
  15. }

Operating results:
wVersion: 2.2
wHighVersion: 2.2
szDescription: WinSock 2.0
szSystemStatus: Running

highest version ws2_32.dll support for 2.2, it is recommended to use version 2.2 also.

To sum up: The first step is to load the WinSock programming ws2_32.dll, then call WSAStartup () function to initialize the version number and indicate you want to use.

Guess you like

Origin blog.csdn.net/Qsir/article/details/93738902