C# 程序,用于查找客户端 IP 地址
首先使用 C# 中的 Dns.GetHostName() 方法查找主机名 -
String hostName = string.Empty; hostName = Dns.GetHostName(); Console.WriteLine("Hostname: "+hostName);
现在使用 IPHostEntry.AddressList 属性查找 IP 地址 -
IPHostEntry myIP = Dns.GetHostEntry(hostName); IPAddress[] address = myIP.AddressList;
示例
尝试以下代码显示 IP 地址 -
using System; using System.Net; class Program { static void Main() { String hostName = string.Empty; hostName = Dns.GetHostName(); IPHostEntry myIP = Dns.GetHostEntry(hostName); IPAddress[] address = myIP.AddressList; for (int i = 0; i < address.Length; i++) { Console.WriteLine("IP Address {1} : ",address[i].ToString()); } Console.ReadLine(); } }
广告