Qt CLI 02 IP

This project was created to obtain basic data from your computer’s network cards. Two things to keep in mind:
1) Your computer may have more than one network card.
2) There are two versions of IP addresses.

You need to include the Network library, so you’ll see some small changes you’ll need to make in CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)

project(QtCLI02_IP LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Network)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network)

add_executable(QtCLI02_IP
main.cpp
)
target_link_libraries(QtCLI02_IP Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network)

include(GNUInstallDirs)
install(TARGETS QtCLI02_IP
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

The following modules will be included in main.cpp for later use:

#include <QCoreApplication>



#include <QHostAddress>
#include <QNetworkInterface>
#include <QAbstractSocket>


#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //A list will be created with the selected information
    //Each node in the list will be a network card
    //The QHostAddress and QNetworkInterface modules are used here
    QList<QHostAddress> list = QNetworkInterface::allAddresses();

    for(int i=0;i< list.count();i++){

        QHostAddress nic = list.at(i);

        //Gets the data from the network card "i"
        qInfo() << nic.toString();

        //Check if it is loopback
        if (nic.isLoopback())
            qInfo() << "\tLoopback" ;

        //Check if it is multicast
        if (nic.isMulticast())
            qInfo() << "\tMulticast";

        //Now the protocol used will be checked
            switch(nic.protocol()){

            case QAbstractSocket::UnknownNetworkLayerProtocol:
                qInfo() << "\tProtocol:Unknown";
                break;
            case QAbstractSocket::AnyIPProtocol:
                qInfo() << "\tProtocol: Any";
                break;
            case QAbstractSocket::IPv4Protocol:
                qInfo() << "\tProtocol: IPv4";
                break;
            case QAbstractSocket::IPv6Protocol:
                qInfo() << "\tProtocol:IPv6";
                break;
            }
    }

    return a.exec();
}

and that’s all folks!!!

grafeno30

Computer science teacher, former analyst/programmer for the Andalusian Regional Government. He holds a degree in computer engineering from the Higher Technical School of Seville.

Ver todas las entradas