我没有以IPv4格式获取主机IP地址,而是获得了IPv6格式 [英] Instead of get the host IP address in IPv4 format, i got IPv6 format
本文介绍了我没有以IPv4格式获取主机IP地址,而是获得了IPv6格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如本主题所述,
我试图获取主机IP地址,
该代码运行完美,我得到了意外的结果...
我不是用IPv4格式的IP地址,而是用IPv6格式的IP地址!
我该如何克服这个问题?
As mention in the subject,
i was trying to get the host IP address,
the code run perfectly , and i got an unexpected results...
Instead of IP address in IPv4 format , i got it in IPv6 format!
How shall i overcome this issue?
try
{
//iphostname = Dns.GetHostName(); // Resolving Host name
IPHostEntry ipentry = Dns.GetHostEntry(hostLabel.Text);
IPAddress[] addr = ipentry.AddressList;// Resolving IP Addresses
for (int i = 0; i < addr.Length; i++)
{
try
{
ipLabel.Text= Convert.ToString(addr[i]) + "\r\n";
}
catch
{
ipLabel.Text += "IP Address | " + "\r\n";
}
}
}
catch
{
| " + "\r\n";
}
}
屏幕截图参考: https://lh6. googleusercontent.com/-vn4v728bOQI/UCNu_rlz6II/AAAAAAAAANI/UYbtgxNqt-0/s616/Fullscreen+capture+982012+40215+PM.jpg [
Screenshot reference : https://lh6.googleusercontent.com/-vn4v728bOQI/UCNu_rlz6II/AAAAAAAAANI/UYbtgxNqt-0/s616/Fullscreen+capture+982012+40215+PM.jpg[^]
推荐答案
在此处查看示例: http://msdn. microsoft.com/en-us/library/system.net.ipaddress.aspx [
Look at the sample here: http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx[^]
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
ipLabel.Text = addr[addr.Length - 2].ToString();
这篇关于我没有以IPv4格式获取主机IP地址,而是获得了IPv6格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文