Sample code for 30+ languages & platforms
C

Get Local Network Adapter Addresses

See more Socket/SSL/TLS Examples

Demonstrates Socket.GetAdaptersAddresses, which populates a JsonObject with information about local network adapters, including IPv4 addresses, IPv6 addresses, and MAC addresses.

Tip: Code to parse the returned JSON can be generated with Chilkat's online tool at https://tools.chilkat.io/jsonParse.

Background. This provides a portable way to enumerate the host's network interfaces and their addresses.

Chilkat C Downloads

C
#include <C_CkSocket.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSocket socket;
    HCkJsonObject json;
    const char *jsonStr;

    success = FALSE;

    socket = CkSocket_Create();

    //  Populate a JsonObject with information about the local network adapters, including IPv4 addresses,
    //  IPv6 addresses, and MAC addresses.
    json = CkJsonObject_Create();
    success = CkSocket_GetAdaptersAddresses(socket,json);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(socket));
        CkSocket_Dispose(socket);
        CkJsonObject_Dispose(json);
        return;
    }

    CkJsonObject_putEmitCompact(json,FALSE);
    jsonStr = CkJsonObject_emit(json);
    printf("%s\n",jsonStr);
    //  JSON parsing code for this result can be generated at Chilkat's online tool:
    //  https://tools.chilkat.io/jsonParse


    CkSocket_Dispose(socket);
    CkJsonObject_Dispose(json);

    }