Skip to content

Instantly share code, notes, and snippets.

@SamKr
Last active January 6, 2020 14:03
Show Gist options
  • Save SamKr/064c5f22383fad625ad5d45cef14cb86 to your computer and use it in GitHub Desktop.
Save SamKr/064c5f22383fad625ad5d45cef14cb86 to your computer and use it in GitHub Desktop.
Gets the current active IP address
using (var s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp))
{
s.Bind(new IPEndPoint(IPAddress.Any, 0));
s.Connect("google.com", 0);
var ipaddr = s.LocalEndPoint as IPEndPoint;
var addr = ipaddr?.Address?.ToString() ?? "";
Console.WriteLine(addr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment