(2)Initializing Winsock
Before calling Winsock functions we must initialize the use of the winsock DLL.
①Create a WSADATA object called wsaData
②Call WSAStartup and return its value as an integer and check for errors.
①Create a WSADATA object called wsaData
②Call WSAStartup and return its value as an integer and check for errors.
WSADATA wsadata;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
Node: The WSAStartup function is called to initiate use of WS2_32.dll. The WSADATA structure contains information about the Windows Sockets implementation. The MAKEWORD(2,2) parameter of WSAStartup makes a request for version 2.2 of Winsock on the system, and sets the passed version as the highest version of Windows Sockets support that the caller can use.