How does WSAStartup function initiates use of the Winsock DLL? How does WSAStartup function initiates use of the Winsock DLL? c c

How does WSAStartup function initiates use of the Winsock DLL?


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.