The DNS method is simple and practical to obtain the IP address of a server, as follows:
The following is a reference clip:
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:
The following is a reference clip:
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:
The following is a reference clip:
Using system. net;
Textip. Text = page. Request. userhostaddress;
To obtain the MAC address of the client's local machine, it is more complicated to import and call two APIs and use ARP to obtain the MAC address of the machine in the same network segment, for cross-network profit, use the IP scan or the nbtstat command in cmd to obtain the MAC address. You can also obtain it by reading the system registry value or WMI database.