Last active
January 6, 2020 14:03
-
-
Save SamKr/064c5f22383fad625ad5d45cef14cb86 to your computer and use it in GitHub Desktop.
Gets the current active IP address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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