The DNS method is relatively simple and practical to obtain the IP address of the server, as follows:
The following is a quote:
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 connection available";
}
}
Another way to obtain the server's IP address and MAC address is as follows:
The following is a quote:
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();
}
}
Obtaining the IP address of the client's local machine is quite simple. The method is as follows:
The following is a quoted fragment:
using System.Net;
TextIP.Text=Page.Request.UserHostAddress;
If you want to obtain the MAC address of the client's local machine, it is relatively complicated. You have to import and call two APIs and obtain it using the ARP protocol. However, this can only obtain the MAC address of the machine on the same network segment. For cross-network segments, you need to use IP scanning or Use the nbtstat command in cmd to obtain the MAC address. It can also be obtained by reading the system registry value or WMI database. If you have any simple and feasible methods, please leave a message and let me know.