WSASTARTUP()
this function is used to initialize the Winsock library, which provides the API for socket programming in Windows. This function must be called before using any socket-related functions such as socket(), bind(), connect(), etc. It sets up the internal data structures and loads the appropriate version of the Winsock DLL for network communication.
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA d;
// Initialize Winsock version 2.2
if (WSAStartup(MAKEWORD(2, 2), &d)) {
printf("Failed to initialize.\n");
return -1;
}
WSACleanup();
printf("Ok.\n");
return 0;
}
* the WSADATA structure will be filled in by WSAStartup() with details about the
Windows Sockets implementation. The WSAStartup() function returns 0 upon success,
and non-zero upon failure.
* for Visual Studio the #pragma comment(lib, "ws2_32.lib") tells Microsoft Visual C to
link the executable with the Winsock library, ws2_32.lib.
- this is ignored on MinGW
- to explicitly tell the MinGW compiler to link in the library, add the
cmdline option, -lws2_32.
- gcc win_init.c -o win_init.exe -lws2_32
Last updated