Qt/C++ : How to get remote PC (communication peer) MAC address?

前端 未结 3 769
轻奢々
轻奢々 2021-01-07 14:27

I am using Qt5 on Windows 7.
In my application (TCP server), I am currently using some methods from QTcpSocket class:
- QAbstractSocket::peerAddress(

3条回答
  •  甜味超标
    2021-01-07 15:17

    If you can run code on remote peer, MAC address can be reported with hardwareAddress() call of the interface.

    For example , to report MAC address of WiFi interface, and all IPv4 addresses on that intrface:

    for(const QNetworkInterface& iface: QNetworkInterface::allInterfaces()){
        if (iface.type() == QNetworkInterface::Wifi){
            qDebug() << "MAC:" << iface.hardwareAddress();
            for (const QHostAddress& addr : iface.allAddresses()){
                if (addr.protocol() == QAbstractSocket::IPv4Protocol)
                    qDebug() << "IPv4 Addr: " << addr;
            }
        }
    }
    

提交回复
热议问题