WSAStartup

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

Syntax

int WSAStartup( WORD wVersionRequested, LPWSADATA lpWSAData );

Parameters

Parameter Description
wVersionRequested The version of the Windows Sockets API that the calling application expects to use. This parameter is a bitmask that can be constructed using the `MAKEWORD` macro. The high byte of wVersionRequested specifies the minor version number, and the low byte specifies the major version number.
lpWSAData A pointer to a WSADATA structure that receives details about the Windows Sockets implementation. The application should not set any fields in this structure; only the Winsock DLL writes to it.

Return Value

Success

If the function succeeds, the return value is zero.

Failure

If the function fails, the return value is one of the Windows Sockets error codes.

Remarks

An application must successfully call WSAStartup before it can use any other functions in the Winsock DLL. A successful call to WSAStartup must be matched by a corresponding call to WSACleanup.

The wVersionRequested parameter is used by the application to specify the Winsock version it requires. The Winsock DLL checks if it can support the requested version. If the DLL supports the requested version or a higher version, it initializes itself for the highest minor version supported that is less than or equal to the requested minor version.

After a successful call to WSAStartup, the application can use the information returned in the WSADATA structure to determine the Winsock implementation details.

If the Winsock DLL cannot support the version requested by the application, WSAStartup returns an error code indicating this failure.

It is important to note that the Winsock DLL is initialized on a per-process basis. Multiple threads within the same process can call WSAStartup, but this does not result in multiple initializations. The Winsock DLL keeps an internal count of how many times WSAStartup has been called. Each successful call to WSAStartup must be balanced by a call to WSACleanup.

Example

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

int main() {
    WSADATA wsaData;
    int iResult;

    // Make sure to use version 2.2 or higher
    const WORD wVersionRequested = MAKEWORD(2, 2);

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

    printf("Winsock initialized successfully!\n");
    printf("Winsock version: %d.%d\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
    printf("Description: %s\n", wsaData.szDescription);

    // ... perform Winsock operations ...

    // Clean up Winsock
    WSACleanup();
    printf("Winsock cleaned up.\n");

    return 0;
}

Requirements

Attribute Value
Minimum supported client Windows 2000 Professional, Windows XP
Minimum supported server Windows 2000 Server
Product Windows Sockets 1.1, Windows Sockets 2
Header Winsock2.h
Library Ws2_32.lib
DLL Ws2_32.dll