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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.