Events

:

:

Elektronik | Funk | Software

Der Technik-Blog

  • Social Media

    Werbung:


    New Posts


    Events

    • Keine zukünftigen Events vorhanden

    The Tech-Blog

    C++ IP Address View

    C++ example code to display IP addresses

    Alex @ AEQ-WEB

    The following CMake C++ sample code shows the IP addresses of the network cards under Windows and Linux.

    Werbung:

    # Set the minimum required version of CMake
    cmake_minimum_required(VERSION 3.10)
    
    # Project name
    project(IpView)
    
    # Set the C++ standard
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED True)
    
    # Create the executable program
    add_executable(IpView src/main.cpp)
    
    # Link necessary libraries for Windows
    if (WIN32)
        target_link_libraries(IpView ws2_32 iphlpapi)
        set(CMAKE_CXX_FLAGS "-static-libgcc -static-libstdc++")
        set(CMAKE_EXE_LINKER_FLAGS "-static")
    endif()
    
    # Enable static linking on Linux
    if (UNIX AND NOT APPLE)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++ -static")
        target_link_libraries(IpView -static -pthread)
    endif()
    

    Werbung:

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <iomanip>
    #include <chrono>
    #include <ctime>
    #include <algorithm>
    
    #ifdef _WIN32
        #include <winsock2.h>
        #include <iphlpapi.h>
        #pragma comment(lib, "iphlpapi.lib")
        #pragma comment(lib, "Ws2_32.lib")
        #include <ws2tcpip.h>
    #else
        #include <sys/types.h>
        #include <sys/socket.h>
        #include <ifaddrs.h>
        #include <netinet/in.h>
        #include <arpa/inet.h>
        #include <unistd.h>
    #endif
    
    bool initializeNetwork() {
    #ifdef _WIN32
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
            std::cerr << "WinSock could not be initialized." << std::endl;
            std::cin.get(); // Wait for input to keep window open on error
            return false;
        }
        std::cout << "WinSock successfully initialized." << std::endl;
    #endif
        return true;
    }
    
    void printIPAddresses() {
    #ifdef _WIN32
        // Retrieve adapter information on Windows
        ULONG bufferSize = 15000;
        PIP_ADAPTER_ADDRESSES adapterAddresses = (IP_ADAPTER_ADDRESSES*)malloc(bufferSize);
        if (GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, adapterAddresses, &bufferSize) == NO_ERROR) {
            for (PIP_ADAPTER_ADDRESSES adapter = adapterAddresses; adapter != nullptr; adapter = adapter->Next) {
                if (adapter->OperStatus == IfOperStatusUp) {
                    for (PIP_ADAPTER_UNICAST_ADDRESS ua = adapter->FirstUnicastAddress; ua != nullptr; ua = ua->Next) {
                        char ipAddress[INET_ADDRSTRLEN];
                        sockaddr_in* sockaddr = (sockaddr_in*)ua->Address.lpSockaddr;
                        inet_ntop(AF_INET, &sockaddr->sin_addr, ipAddress, INET_ADDRSTRLEN);
                        if (std::string(ipAddress) != "127.0.0.1") {
                            std::cout << "IP Address: " << ipAddress << std::endl;
                        }
                    }
                }
            }
        } else {
            std::cerr << "Could not retrieve adapter information." << std::endl;
            std::cin.get(); // Wait for input to keep window open on error
        }
        free(adapterAddresses);
        WSACleanup();
    #else
        struct ifaddrs *interfaces = nullptr;
        struct ifaddrs *ifa = nullptr;
        if (getifaddrs(&interfaces) == -1) {
            std::cerr << "Could not retrieve network adapters." << std::endl;
            std::cin.get(); // Wait for input to keep window open on error
            return;
        }
        for (ifa = interfaces; ifa != nullptr; ifa = ifa->ifa_next) {
            if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
                char ipAddress[INET_ADDRSTRLEN];
                void* addr = &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr;
                inet_ntop(AF_INET, addr, ipAddress, INET_ADDRSTRLEN);
                if (std::string(ipAddress) != "127.0.0.1") {
                    std::cout << "IP Address: " << ipAddress << std::endl;
                }
            }
        }
        freeifaddrs(interfaces);
    #endif
    }
    
    
    
    int main() {
        std::cout << "IPViewer" << std::endl;
    
        if (!initializeNetwork()) {
            std::cerr << "Network access not available." << std::endl;
            std::cin.get();
            return 1;
        }
    
        std::cout << "Network access successful. Retrieving IPs:" << std::endl;
        printIPAddresses();
        std::cin.get();
        return 0;
    }
    


    Info: This page was automatically translated and may contain errors
    122X122

    About the Author

    Alex, the founder of AEQ-WEB. He works for more of 10 years with different computers, microcontroller and semiconductors. In addition to hardware projects, he also develops websites, apps and software for computers.

    Top articles in this category:

    Vaisala RS41 Radiosonde Firmware Flash

    Radiosonde RS41 Firmware Flash

    • Video
    • DE/EN

    Every day hundreds of meteorological radiosondes fall from the sky. In this article we convert a radiosonde into a GPS tracker for APRS, RTTY & CW

    read more
    SenseCAP T1000 Payload Decoder

    SenseCAP T1000 Payload Decoder

    • DE/EN

    Alternative payload decoder for the SenseCAP T1000 LoRaWAN GPS tracker. Compatible with TTN Mapper, LoWTrack and other apps.

    read more

    Social Media

    Werbung:


    New Posts


    Events

    • Keine zukünftigen Events vorhanden