具体方法为如下:
private void btnChenk_Click(object sender, System.EventArgs e)
{ System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; if ( addressList.Length>1) { nativeIP.Text = addressList[0].ToString(); serverIP.Text = addressList[1].ToString(); } else { nativeIP.Text = addressList[0].ToString(); serverIP.Text = "Break the line..."; } }通常addressList[]所得到的列表就是当前的IP地址;
如果当前是拨号上网的话addressList[0].ToString()就是
本地IP地址;addressList[1].ToString() 就是上网地址。
如果当前是使用[EnterNet 300]拨号上网的话addressList[1].ToString()
就是本地IP地址;addressList[0].ToString() 就是上网地址。
如果程序只能够检测到只有一个IP地址的话,证明当前
电脑没有在线!
获取客户端IP:
Code 1public string GetUserIP() 2 { 3 string _userIP; 4 if(Request.ServerVariables["HTTP_VIA"] == null) 5 { 6 userIP= Request.UserHostAddress; 7 } 8 else 9 { 10 userIP= Request.ServerVariables["HTTP_X_FORWARDED_FOR"];11 }12 return userIP;13 }14
获取客户端MAC地址:
Code 1[DllImport("Iphlpapi.dll")] 2 private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length); 3 [DllImport("Ws2_32.dll")] 4 private static extern Int32 inet_addr(string ip); 5 6 private void Page_Load(object sender, System.EventArgs e) 7 { 8 // 在此处放置用户代码以初始化页面 9 try10 { 11 string userip=Request.UserHostAddress;12 string strClientIP = Request.UserHostAddress.ToString().Trim();13 Int32 ldest = inet_addr(strClientIP); //目的地的ip 14 Int32 lhost = inet_addr(""); //本地服务器的ip 15 Int64 macinfo = new Int64(); 16 Int32 len = 6; 17 int res = SendARP(ldest,0, ref macinfo, ref len);18 string mac_src=macinfo.ToString("X");19 if(mac_src == "0")20 { 21 if(userip=="127.0.0.1")22 Response.Write ("正在访问Localhost!");23 else24 Response.Write ("欢迎来自IP为" + userip + "的朋友!" + "<br>");25 return;26 }2728 while(mac_src.Length<12)29 { 30 mac_src = mac_src.Insert(0,"0");31 }3233 string mac_dest="";3435 for(int i=0;i<11;i++)36 { 37 if (0 == (i % 2))38 { 39 if ( i == 10 )40 { 41 mac_dest = mac_dest.Insert(0,mac_src.Substring(i,2));42 }43 else44 { 45 mac_dest ="-" + mac_dest.Insert(0,mac_src.Substring(i,2));46 }47 } 48 }4950 Response.Write ("欢迎来自IP为"+userip+ "<br>" + ",MAC地址为"+mac_dest+"的朋友!" 5152 + "<br>");53 }54 catch(Exception err) 55 { 56 Response.Write(err.Message);57 }58 }59
获取服务器IP一:
1private void ButtonIP_Click(object sender, System.EventArgs e) 2{ 3 Syste m.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; 4if ( addressList.Length>1) 5{ 6TextLIP.Text = addressList[0].ToString(); 7TextSIP.Text = addressList[1].ToString(); 8} 9else10{ 11TextLIP.Text = addressList[0].ToString();TextSIP.Text = "没有可用的连接";12}13} 1415
获取服务器IP及MAC地址二:
using System.Management; string stringMAC = ""; string stringIP = ""; ManagementClass MC = new ManagementClass "Win32_NetworkAdapterConfiguration"); ManagementObjectCollection MOC= MC.GetInstances(); foreach(ManagementObject MO in MOC) { if ((bool)MO["IPEnabled"] == true){stringMAC += MO["MACAddress"].ToString(); TextMAC.Text = stringMAC.ToString(); string[] IPAddresses = (string[]) MO["IPAddress"];if(IPAddresses.Length > 0) stringIP = IPAddresses[0];TextIP.Text = stringIP.ToString(); } }
命运负责洗牌,但是玩牌的是我们自己!