Thursday, March 31, 2022

IP Detect

string IPAddress = "";

IPHostEntry Host = default(IPHostEntry);

string Hostname = null;

Hostname = System.Environment.MachineName;

Host = Dns.GetHostEntry(Hostname);

foreach (IPAddress IP in Host.AddressList)

{

if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)

{

IPAddress = Convert.ToString(IP);

}

}


=====================================



 string url = "http://checkip.dyndns.org";

System.Net.WebRequest req = System.Net.WebRequest.Create(url);

System.Net.WebResponse resp = req.GetResponse();

System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());

string response = sr.ReadToEnd().Trim();

string[] ipAddressWithText = response.Split(':');

string ipAddressWithHTMLEnd = ipAddressWithText[1].Substring(1);

string[] ipAddress = ipAddressWithHTMLEnd.Split('<');

string mainIP = ipAddress[0];



============


Socket s;


IPEndPoint remoteIpEndPoint = s.RemoteEndPoint as IPEndPoint;

IPEndPoint localIpEndPoint = s.LocalEndPoint as IPEndPoint;


string local = "";

string remote = "";


if (remoteIpEndPoint != null)

{

// Using the RemoteEndPoint property.

remote = remoteIpEndPoint.Address.ToString(); 

}


if (localIpEndPoint != null)

{

// Using the LocalEndPoint property.

local = localIpEndPoint.Address.ToString();

}

No comments:

Post a Comment