c Two ways to get the local ip address on the LAN
- 2020-06-03 08:11:39
- OfStack
/// <summary>
/// Gets the native on the LAN IP address
/// </summary>
/// <returns></returns>
private string GetLocalIPAddress()
{
System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
string strNativeIP = "";
string strServerIP = "";
if (addressList.Length > 1)
{
strNativeIP = addressList[0].ToString();
strServerIP = addressList[1].ToString();
}
else if(addressList.Length==1)
{
strServerIP = addressList[0].ToString();
}
return strServerIP;
}
Another is to crawl the web page query to the address of the IP to achieve. The implementation is as follows:
/// <summary>
/// Get access to the Internet on your machine IP
/// </summary>
/// <returns></returns>
private string GetConnectNetAddress()
{
string strUrl = "http://www.ip138.com/ip2city.asp"; // To obtain IP The website of
Uri uri = new Uri(strUrl);
WebRequest webreq = WebRequest.Create(uri);
Stream s = webreq.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); // Read the data returned from the site Format: Yours IP The address is: [x.x.x.x]
int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "").Replace("<", "");
return ip;
}