zoukankan      html  css  js  c++  java
  • QT5下获取本机IP地址、计算机名、网络连接名、MAC地址、子网掩码、广播地址

    获取主机名称

    /*
     * 名称:get_localmachine_name
     * 功能:获取本机机器名称
     * 参数:no
     * 返回:QString
     */
    QString CafesClient::get_localmachine_name()
    {
        QString machineName     = QHostInfo::localHostName();
        return machineName;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    获取本机IP地址

    /*
     * 名称:get_localmachine_ip
     * 功能:获取本机的IP地址
     * 参数:no
     * 返回:QString
     */
    QString CafesClient::get_localmachine_ip()
    {
        QString ipAddress;
        QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
        // use the first non-localhost IPv4 address
        for (int i = 0; i < ipAddressesList.size(); ++i) {
            if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
                ipAddressesList.at(i).toIPv4Address()) {
                ipAddress = ipAddressesList.at(i).toString();
                break;
            }
        }
        // if we did not find one, use IPv4 localhost
        if (ipAddress.isEmpty())
            ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
        return ipAddress;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    获取本机网络连接名、MAC地址

    /*
     * 名称:get_localmachine_mac
     * 功能:获取本机的MAC地址
     * 参数:no
     * 返回:void
     */
    QString CafesClient::get_localmachine_mac()
    {
        QList<QNetworkInterface> nets       = QNetworkInterface::allInterfaces();
        int i = 0;
        foreach(QNetworkInterface ni,nets)
        {
            i++;
            qDebug()<<i<<ni.name()<<ni.hardwareAddress()<<ni.humanReadableName();
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    获取本机子网掩码、广播地址

    //在上个函数的环境下
    QList<QNetworkAddressEntry> entryList =interface.addressEntries();
    //获取IP地址条目列表,每个条目中包含一个IP地址,一个子网掩码和一个广播地址
    foreach(QNetworkAddressEntry entry,entryList)
    {
        //遍历每一个IP地址条目
    qDebug()<<”IP Address:
                “<<entry.ip().toString();
        //IP地址
    qDebug()<<”Netmask:
                “<<entry.netmask().toString();
        //子网掩码
    qDebug()<<”Broadcast:
                “<<entry.broadcast().toString();
        //广播地址
    }

    http://blog.csdn.net/u013007900/article/details/50444459
  • 相关阅读:
    第二十二节:类与对象后期静态绑定对象和引用
    WePHP的表单令牌验证
    第二十一节:类与对象对象的比较类型约束
    Windows下 C++ 实现匿名管道的读写操作
    Mongoose 利用实现HTTP服务
    C++ Qt 框架静态编译 操作记录
    使用Qt框架开发http服务器问题的记录
    把CMD下的color 方案遍历一遍
    C++学习笔记
    在1~10的整数范围随机5个不重复的整数
  • 原文地址:https://www.cnblogs.com/findumars/p/5107610.html
Copyright © 2011-2022 走看看