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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM