由于Winsock在被调用时是以动态链接库Winsock.dll的形式实现的,所以在它初始化时应首先调用WSAStartup函数,对Winsock DLL进行初始化,确定被调用的Winsock的版本号,并以此分配必要的资源:
int WSAStartup(
__in WORD wVersionRequested, //用于存储要加载的Winsock库的版本;一般高位字节用于存储
//Winsock库的副版本,而低位字节则用来存储主版本,
//使用MAKEWORD(X, Y)构造一个完整的版本信息
__out LPWSADATA lpWSAData //指向WSAData结构的指针,该结构包含了加载库版本的相关信息
);
返回值:
成功时,返回0;
失败时,直接返回错误码,不用也不能再调用WSAGetLastError()函数

typedef struct WSAData {
WORD wVersion; //当前使用的版本信息
WORD wHighVersion; //Winsock库最高版本号
char szDescription[WSADESCRIPTION_LEN+1]; //闲置不用
char szSystemStatus[WSASYS_STATUS_LEN+1]; //闲置不用
unsigned short iMaxSockets; //同时打开的套接字个数
unsigned short iMaxUdpDg; //数据包最大长度
char *lpVendorInfo; //为指定厂商信息预留
} WSADATA, *LPWSADATA;

WSACleanup函数用于终止对Winsock DLL的使用,并释放资源:
int WSACleanup(void);
返回值:
成功时,返回0;
失败时,返回SOCKET_ERROR,调用WSAGetLastError函数可查看进一步错误信息。
在多线程环境中,WSACleanup终止所有线程的socket操作。

实例代码如下:
#pragma comment(lib, "wininet.lib")

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>

int __cdecl main()
{

WORD wVersionRequested;
WSADATA wsaData;
int err;

/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d/n", err);
return 1;
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */

if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("Could not find a usable version of Winsock.dll/n");
WSACleanup();
return 1;
}
else
printf("The Winsock 2.2 dll was found okay/n");


/* The Winsock DLL is acceptable. Proceed to use it. */

/* Add network programming using Winsock here */

/* then call WSACleanup when down using the Winsock dll */

WSACleanup();
}




附:
MAKEWORD宏用于拼接两个指定的BYTE值,得到一个WORD值:
WORD MAKEWORD(
BYTE bLow, //新值的低位字节
BYTE bHigh //新值的高位字节
);
返回值是一个WORD值。

LOBYTE宏用于从指定的WORD值获取其低位字节:
BYTE LOBYTE(
WORD wValue
);

HIBYTE宏用于从指定的WORD值获取其高位字节:
BYTE HIBYTE(
WORD wValue
);