C 獲取本機IP位址(完整原始碼)

2021-06-21 11:23:29 字數 889 閱讀 5025

親身實驗過的兩種方法,都可行。

以下**為c#控制台程式**。

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.net; //使用方法一需要用到

using system.net.sockets; //使用方法二需要用到

namespace getlocalip

/*方法二:

* 下面的addresslist是乙個ipaddress型別,這裡面存放了系統的所有ip位址,有ipv4的,有ipv6的,

* 還有不同網絡卡的也會在這裡面。在win7下預設啟用了ipv6,上面這段**返回的是ipv6格式的位址,

* 而且根據系統情況不同,ipv4是不能確定存放在陣列的哪乙個下標中,

*/ console.writeline();

console.writeline("方法二:");

string hostname = dns.gethostname(); //得到主機名

console.writeline("主機名:"+hostname);

iphostentry ipentry = dns.gethostentry(hostname);

for (int i = 0; i < ipentry.addresslist.length; i++)

}/* 返回所有位址,v4、v6以及其他網絡卡位址

for (int i = 0; i < ipentry.addresslist.length; i++)*/}

}}

c 獲取本機IP位址

try catch exception ex 但是以上獲取ip位址的方法,可能獲取不到你想要的ip位址,比如,你本機開啟了虛擬機器,vpn等,都會增加ip位址 string localip using socket socket new socket addressfamily.internetwo...

獲取本機IP位址

cstring strlocalip char szhostname 64 memset szhostname,0x00,sizeof szhostname gethostname szhostname,sizeof szhostname phostent phost gethostbyname s...

獲取本機IP位址

第一步 載入winsock2靜態鏈結庫檔案。include pragma comment lib,ws2 32.lib bool loadwinsock 第二步 獲取本地的主機名稱。hostent phostent null char szhostname 256 memset szhostname...