Winsock WSAStartup Function

Summary

The WSAStartup function initiates the use of the Windows Sockets DLL. Before any other Windows Sockets function can be called, a Winsock application or DLL must successfully call WSAStartup.

Syntax

int WSAStartup(
  [in]  WORD                 wVersionRequested,
  [out] LPWSADATA            lpWSAData
);
            
Parameters
Return Value
On success, WSAStartup returns zero.

On failure, the return value is one of the Winsock error codes listed below.
Remarks

Every application or DLL that calls the Winsock interface must call WSAStartup one or more times before calling any other Winsock functions. It is recommended that an application call WSAStartup once during its initialization.

The wVersionRequested parameter should be set to the highest version of the Windows Sockets specification that can be supported by the DLL. The highest supported version by the Winsock implementation is a value between 2.0 and 2.2.

When WSAStartup returns successfully, the WSADATA data structure pointed to by lpWSAData is filled with information about the Winsock implementation.

The calling process can call WSAStartup multiple times, as long as it also calls WSACleanup an equal number of times. The Winsock DLL keeps a count of how many times WSAStartup has been called. The DLL only performs its actual initialization tasks the first time it is called. It increments its internal counter for each successful call to WSAStartup.

To allow an application to specify the Winsock version it needs, the WSAStartup function supports the use of the MAKEWORD macro, defined in Winsock2.h, as shown in the following example:


WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD(2, 2); // Request Winsock version 2.2
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
    // Tell the user that Winsock DLL could not be found or is not accessible
    // or that the desired version of Winsock is not supported.
    fprintf(stderr, "WSAStartup failed with error %d\n", err);
    // handle error
}

// ... other Winsock functions can be called now ...

// When done, call WSACleanup
// WSACleanup();
            
Requirements
Client

Requires Winsock 1.1.

Server

Requires Winsock 1.1.

Header

Winsock2.h (include Winsock.h)

Library

Ws2_32.lib

DLL

Ws2_32.dll

See Also

WSACleanup

WSADATA

Winsock Reference