Listing network adapters on Windows

The Windows networking API is called Winsock, and we will go into much more detail about it in the next chapter.

Whenever we are using Winsock, the first thing we must do is initialize it. This is done with a call to WSAStartup(). Here is a small C program, win_init.c, showing the initialization and cleanup of Winsock:

/*win_init.c*/#include <stdio.h>#include <winsock2.h>#pragma comment(lib, "ws2_32.lib") int main() {    WSADATA d;     if (WSAStartup(MAKEWORD(2, 2), &d)) {        printf("Failed to initialize.\n");        return -1;    }     WSACleanup();    printf("Ok.\n");    return 0;}

The WSAStartup() function is called with the requested version, Winsock 2.2 in this case, and a WSADATA structure. The WSADATA structure will be filled ...

Get Hands-On Network Programming with C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.