原帖:

引入命名空间:

 

 
  1. using System.Net; 

 

完整代码:

 
  1. namespace IPExample 
  2.     public partial class Form1 : Form 
  3.     { 
  4.         public Form1() 
  5.         { 
  6.             InitializeComponent(); 
  7.         } 
  8.  
  9.         /// <summary> 
  10.         /// 获取本级IP信息 
  11.         /// </summary> 
  12.         /// <param name="sender"></param> 
  13.         /// <param name="e"></param> 
  14.         private void btn_ShowHostIPInfo_Click(object sender, EventArgs e) 
  15.         { 
  16.             lst_HostIPInfo.Items.Clear(); 
  17.             string name = Dns.GetHostName(); 
  18.             lst_HostIPInfo.Items.Add("本机主机名:" + name); 
  19.             IPHostEntry me = Dns.GetHostEntry(name); 
  20.             lst_HostIPInfo.Items.Add("本机所有IP地址:"); 
  21.             foreach (IPAddress ip in me.AddressList) 
  22.             { 
  23.                 lst_HostIPInfo.Items.Add(ip); 
  24.             } 
  25.             IPAddress localIp = IPAddress.Parse("127.0.0.1"); 
  26.             IPEndPoint iep = new IPEndPoint(localIp, 80); 
  27.             lst_HostIPInfo.Items.Add("IP端点:" + iep.ToString()); 
  28.             lst_HostIPInfo.Items.Add("IP端口:" + iep.Port); 
  29.             lst_HostIPInfo.Items.Add("IP地址:" + iep.Address); 
  30.             lst_HostIPInfo.Items.Add("IP地址族:" + iep.AddressFamily); 
  31.             lst_HostIPInfo.Items.Add("可分配端口最大值:" + IPEndPoint.MaxPort); 
  32.             lst_HostIPInfo.Items.Add("可分配端口最小值:" + IPEndPoint.MinPort);  
  33.         } 
  34.  
  35.         /// <summary> 
  36.         /// 获得服务器信息 
  37.         /// </summary> 
  38.         /// <param name="sender"></param> 
  39.         /// <param name="e"></param> 
  40.         private void btn_ShowServerInfo_Click(object sender, EventArgs e) 
  41.         { 
  42.             lst_ServerIPInfo.Items.Clear(); 
  43.             IPHostEntry remoteHost = Dns.GetHostEntry(txt_ServerIPAddress.Text); 
  44.             IPAddress[] remoteIp = remoteHost.AddressList; 
  45.             IPEndPoint iep; 
  46.             foreach (IPAddress ip in remoteIp) 
  47.             { 
  48.                 iep = new IPEndPoint(ip, 80); 
  49.                 lst_ServerIPInfo.Items.Add(iep); 
  50.             }   
  51.         } 
  52.     }