| Reference content is as follows:
DllImport ("Iphlpapi. dll")]
Private static extern int SendARP (Int32 dest, Int32 host, ref Int64 mac, ref Int32 length );
[DllImport ("Ws2_32.dll")]
Private static extern Int32 inet_addr (string ip );
Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page
Try
{
String userip = Request. UserHostAddress;
String strClientIP = Request. UserHostAddress. ToString (). Trim ();
Int32 ldest = inet_addr (strClientIP); // ip address of the destination
Int32 lhost = inet_addr (""); // ip address of the local server
Int64 macinfo = new Int64 ();
Int32 len = 6;
Int res = SendARP (ldest, 0, ref macinfo, ref len );
String mac_src = macinfo. ToString ("X ");
If (mac_src = "0 ")
{
If (userip = "127.0.0.1 ")
Response. Write ("accessing Localhost! ");
Else
Response. Write ("Welcome to friends from" + userip +! "+"
");
Return;
}
While (mac_src.Length <12)
{
Mac_src = mac_src.Insert (0, "0 ");
}
String mac_dest = "";
For (int I = 0; I <11; I ++)
{
If (0 = (I % 2 ))
{
If (I = 10)
{
Mac_dest = mac_dest.Insert (0, mac_src.Substring (I, 2 ));
}
Else
{
Mac_dest = "-" + mac_dest.Insert (0, mac_src.Substring (I, 2 ));
}
}
}
// Method 2
Using System. Text. RegularExpressions;
Using System. Diagnostics;
Public class test
{
Public test
{}
Public static string GetCustomerMac (string IP) // para IP is the client's IP
{
String dirResults = "";
ProcessStartInfo psi = new ProcessStartInfo ();
Process proc = new Process ();
Psi. FileName = "nbtstat ";
Psi. RedirectStandardInput = false;
Psi. RedirectStandardOutput = true;
Psi. Arguments = "-A" + IP;
Psi. UseShellExecute = false;
Proc = Process. Start (psi );
DirResults = proc. StandardOutput. ReadToEnd ();
Proc. WaitForExit ();
DirResults = dirResults. Replace ("\ r", ""). Replace ("\ n", ""). Replace ("\ t ","");
Regex reg = new Regex ("Mac [] {0,} Address [] {0,} = [] {0 ,}(? ((.)*?)) _ MAC ", RegexOptions. IgnoreCase | RegexOptions. Compiled );
Match mc = reg. Match (dirResults + "_ MAC ");
If (mc. Success)
{
Return mc. Groups ["key"]. Value;
}
Else
{
Reg = new Regex ("Host not found", RegexOptions. IgnoreCase | RegexOptions. Compiled );
Mc = reg. Match (dirResults );
If (mc. Success)
{
Return "Host not found! ";
}
Else
{
Return "";
}
}
}
}
This method has to be explored in some places, otherwise it cannot be understood.
//
The DNS method is simple and practical to obtain the IP address of a server, as follows:
Using System. Net;
Private void ButtonIP_Click (object sender, System. EventArgs e)
{
System. Net. IPAddress [] addressList = Dns. GetHostByName (Dns. GetHostName (). AddressList;
If (addressList. Length> 1)
{TextLIP. Text = addressList [0]. ToString ();
TextSIP. Text = addressList [1]. ToString ();
}
Else
{
TextLIP. Text = addressList [0]. ToString ();
TextSIP. Text = "no available connections ";
}
} |