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.

ms738566.wedge(en-us,VS.85).gifTo 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

Related topics

Getting Started With Winsock
Creating a Basic Winsock Application

 

 

Community Additions

ADD
Show:
© 2014 Microsoft