Home > Backend Development > C++ > How to Get the Local Computer's IP Address and Subnet Mask in C ?

How to Get the Local Computer's IP Address and Subnet Mask in C ?

Susan Sarandon
Release: 2024-11-10 03:34:02
Original
372 people have browsed it

How to Get the Local Computer's IP Address and Subnet Mask in C  ?

Determining Local Computer's IP Address and Subnet Mask in C

Acquiring the local computer's IP address and subnet mask is essential for various networking operations. In C , this process can be accomplished by leveraging platform-specific system calls.

1. Unix/MacOS

For Unix and macOS systems, the getifaddrs function allows retrieval of a linked list of network interface addresses. Iterating through this list provides access to each interface's IP address, subnet mask, and other related information:

#include <sys/types.h>
#include <ifaddrs.h>

struct ifaddrs *ifaddr;
int s;

if ((s = getifaddrs(&ifaddr)) == -1) {
    // Error handling
}

for (struct ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
    if (ifa->ifa_addr->sa_family == AF_INET) {
        char addr[INET_ADDRSTRLEN];
        inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, addr, INET_ADDRSTRLEN);
        printf("IP Address: %s\n", addr);

        char netmask[INET_ADDRSTRLEN];
        inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr, netmask, INET_ADDRSTRLEN);
        printf("Subnet Mask: %s\n", netmask);
    }
}
Copy after login

2. Windows

For Windows systems, the GetAdaptersAddresses function provides a similar functionality. This function fills a buffer with information for all network adapters on the local computer:

#include <iphlpapi.h>

PIP_ADAPTER_ADDRESSES addresses;
ULONG outBufLen = 0;
DWORD res = GetAdaptersAddresses(AF_INET, 0, NULL, addresses, &outBufLen);

if (res != ERROR_SUCCESS) {
    // Error handling
}

for (PIP_ADAPTER_ADDRESSES adapter = addresses; adapter != NULL; adapter = adapter->Next) {
    char ip[MAX_PATH + 1];
    InetNtopA(AF_INET, &adapter->FirstUnicastAddress->Address.Ipv4.sin_addr, ip, MAX_PATH + 1);
    printf("IP Address: %s\n", ip);

    char netmask[MAX_PATH + 1];
    InetNtopA(AF_INET, &adapter->FirstUnicastAddress->NetMask.Ipv4.sin_addr, netmask, MAX_PATH + 1);
    printf("Subnet Mask: %s\n", netmask);
}
Copy after login

By following these steps, you can retrieve both the IP address and subnet mask for the local computer in C , allowing you to establish network connections and perform various communication operations effectively.

The above is the detailed content of How to Get the Local Computer's IP Address and Subnet Mask in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template