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

前端 未结 3 738
轻奢々
轻奢々 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:07

    In general, no, this is not possible, since the communication peer may not even have a MAC address (e.g. if it is using networking hardware that isn't based on Ethernet). In particular, information about MAC addresses is not communicated by the IP, TCP, or UDP layers --- those layers use IP addresses instead. So if you want to find out the peer's MAC address you will need to do that at the application level, by having a program on the peer send it to you.

    (One minor exception to the above: If you are communicating via IPv6 and using self-assigned link-local IPv6 addresses (e.g. fe80::blah), it is possible to derive a computer's MAC address from its self-assigned IPv6 address, because the self-assigned IPv6 address is typically derived from the MAC address and contains the MAC address as a subset of its IPv6 address. [Note this won't work across the Internet since link-local addresses are only useful when both machines are located on the same LAN])

    If the remote peer is on the same LAN as the program's host (and the LAN is an Ethernet LAN), then the program might be using some Windows-specific API to look up the IPAddress<->MACAddress mapping in the machine's ARP table. If the remote peer is elsewhere on the Internet, then I don't know how it could do it.

提交回复
热议问题