18

How does WSAStartup function initiates use of the Winsock DLL?

According to the documentation

The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the version of Windows Sockets required and retrieve details of the specific Windows Sockets implementation. The application or DLL can only issue further Windows Sockets functions after successfully calling WSAStartup.

This function initializes WSADATA data structure, but in socket programming we don't pass WSDATA to any function so how does the program comes to know about the Windows Sockets version and other details?

For example in this code

#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32")

void Run(int argc, char* argv[])
{
    char* host = argc < 2 ? "" : argv[1];
    struct hostent* entry = gethostbyname(host);

    if(entry)
    {
        struct in_addr* addr = (struct in_addr*) entry->h_addr;
        printf("IP Address: %s\n", inet_ntoa(*addr));
    }
    else
        printf("ERROR: Resolution failure.\n");
}

int main(int argc, char* argv[])
{
    WSADATA wsaData;

    if(WSAStartup(0x202, &wsaData) == 0)
    {
        Run(argc, argv);
        WSACleanup();
    }
    else
        printf("ERROR: Initialization failure.\n");
}

In this example I am initializing WSADATA data structure using WSAStartup() function and after wards I'm not passing wsaData anywhere.

So how does my program comes to know about wsaData details?

Thanks.

MD XF
  • 7,860
  • 7
  • 40
  • 71
Searock
  • 6,278
  • 11
  • 62
  • 98

1 Answers1

25

WSAStartup has two main purposes.

Firstly, it allows you to specify what version of WinSock you want to use (you are requesting 2.2 in your example). In the WSADATA that it populates, it will tell you what version it is offering you based on your request. It also fills in some other information which you are not required to look at if you aren't interested. You never have to submit this WSADATA struct to WinSock again, because it is used purely to give you feedback on your WSAStartup request.

The second thing it does, is to set-up all the "behind the scenes stuff" that your app needs to use sockets. The WinSock DLL file is loaded into your process, and it has a whole lot of internal structures that need to be set-up for each process. These structures are hidden from you, but they are visible to each of the WinSock calls that you make.

Because these structures need to be set-up for each process that uses WinSock, each process must call WSAStartup to initialise the structures within its own memory space, and WSACleanup to tear them down again, when it is finished using sockets.

Allison Lock
  • 2,375
  • 15
  • 17
  • 1
    In addition, see this link: http://stackoverflow.com/questions/1869689/is-it-possible-to-tell-if-wsastartup-has-been-called-in-a-process on a previous question I asked and discovered the answer through self discovery. – hookenz Feb 14 '11 at 14:16
  • 3
    @Gavin, So why is it that Windows require startup whereas nix environments are fine without? – Pacerier Mar 04 '17 at 11:40
  • 1
    @Pacerier Microsoft. – c00000fd Oct 05 '21 at 18:39
  • 1
    @Pacerier: it is a historical thing mainly. Winsock was originally invented for Windows 3.x, in times before TCP/IP had clearly won out over its competitors (NetBIOS, IPX/SPX, AppleTalk, DECnet, etc). Each protocol stack was a DLL, and WSAStartup read the configuration to decide which DLLs to load. Much of the network stack lived in "user space". Nowadays, there is little use of anything other than TCP/IP, most of which lives in the kernel, the user-space DLLs are just thin layers over the kernel drivers, etc. But the need to call WSAStartup survives because of this history. – Simon Kissane Jan 04 '23 at 21:52