QT5 obtains the local IP address, computer name, network connection name, MAC address, subnet mask, and broadcast address.
Get Host Name
/ *
* Name: get_localmachine_name
* Function: Get local machine name
* Parameter: no
* Returns: QString
* /
QString CafesClient :: get_localmachine_name ()
{
QString machineName = QHostInfo :: localHostName ();
return machineName;
}
Get local IP Address
/ *
* Name: get_localmachine_ip
* Function: Get the IP address of the machine
* Parameter: no
* Returns: QString
* /
QString CafesClient :: get_localmachine_ip ()
{
QString ipAddress;
QList 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;
}
Obtain the network connection name and MAC address of the local machine.
/ *
* Name: get_localmachine_mac
* Function: Get the MAC address of the machine
* Parameter: no
* Returns: void
* /
QString CafesClient :: get_localmachine_mac ()
{
QList nets = QNetworkInterface :: allInterfaces ();
int i = 0;
foreach (QNetworkInterface ni, nets)
{
i ++;
qDebug () <
Obtain the subnet mask and broadcast address of the local machine.
// In the context of the previous function
QList entryList = interface.addressEntries ();
// Get a list of IP address entries, each entry contains an IP address, a subnet mask, and a broadcast address
foreach (QNetworkAddressEntry entry, entryList)
{
// Iterate through each IP address entry
qDebug () << ”IP Address:
"<