Qt獲取本機正在使用的IP(過濾未使用的IP位址)

2021-09-25 10:25:39 字數 959 閱讀 6740

參考1:

參考2:

由於電腦有多個網路適配連線(2個虛擬機器+2個無線網),如圖

// 獲取所有網路介面

qlistinte***ces = qnetworkinte***ce::allinte***ces();

qlistentry;

foreach(qnetworkinte***ce inter, inte***ces)

entry.clear();}}

得到的結果是:

通過呼叫系統命令獲取到ip位址、子網掩碼、閘道器位址(僅當前正在使用的ip位址存在閘道器)

qstring ip = "";

qprocess cmd_pro ;

qstring cmd_str = qstring("ipconfig");

cmd_pro.start("cmd.exe", qstringlist() << "/c" << cmd_str);

cmd_pro.waitforstarted();

cmd_pro.waitforfinished();

qstring result = cmd_pro.readall();

qstring pattern("[0-9]\\.[0-9]\\.[0-9]\\.[0-9]");

qregexp rx(pattern);

int pos = 0;

bool found = false;

while((pos = rx.indexin(result, pos)) != -1)

ip = tmp;

}pos += rx.matchedlength();

}qdebug()<

結果:

QT 獲取本機所有IP

qt 獲取本機所有ip include include include bool islanip uint ip 127.x.x.x給本機位址使用 224.x.x.x為多播位址段 255.255.255.255為通用的廣播位址 10.x.x.x,172.16.x.x和192.168.x.x供本地網使...

Qt獲取本機ip位址

最近有個專案需要手機可以與pc進行tcp傳輸檔案,pc做server時在我的電腦可以,在其他電腦時手機連線超時。檢視了埠,防火牆等等問題未能解決。最後發現是qt在獲取ip位址時獲取錯了,獲取的是第乙個而不是正在使用的。qstring stripaddress qprocess cmd pro qst...

Qt5 12獲取本機IP位址

最近在寫有關qt網路通訊方面,下面是乙個小模組,獲取主機的ip位址。qstring get local ip return 0.0.0.0 首先獲取本機所有ip位址資訊,包括環迴位址如127.0.0.1,以及本機位址如192.168.1.x等等,有安裝虛擬機器的還有其他位址等等。brief 檢測當前...