The DNS method is simple and practical to obtain the IP address of a server, as follows:
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 ";
}
}
Another method to obtain the Server IP address and MAC address is as follows:
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 ();
}
}
It is quite easy to obtain the IP address of the local client. The method is as follows:
Using System. Net;
TextIP. Text = Page. Request. UserHostAddress;
To obtain the MAC address of the local client, it is more complex. You must import and call two APIs and use ARP to obtain the MAC address.
For the MAC address of a machine in the same network segment, use the IP address scan or the nbtstat command in cmd to obtain the MAC address for the profit of cross-network segments. You can also read the System Registration
Table value or WMI database. If you have a simple and feasible solution, please comment below for discussion.