Initializing Winsock

All processes (applications or DLLs) that call Winsock functions must initialize the use of the Windows Sockets DLL before making other Winsock functions calls. This also makes certain that Winsock is supported on the system.

To initialize Winsock

  1. Create a WSADATA object called wsaData.

    WSADATA wsaData;
    
  2. Call WSAStartup and return its value as an integer and check for errors.

    int iResult;
    
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }
    

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.

Next Step for a Client: Creating a Socket for the Client

Next Step for a Server: Creating a Socket for the Server

Getting Started With Winsock

Creating a Basic Winsock Application