private IPAddress GetDefaultIp(IPEndPoint ip)
{
var ping = new Ping();
var options = new PingOptions { Ttl = 1 };
PingReply reply = ping.Send(ip.Address.ToString(), 5000, new byte[32], options);
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface nic in adapters)
{
foreach (GatewayIPAddressInformation gw in nic.GetIPProperties().GatewayAddresses)
{
if (gw.Address.Equals(reply.Address))
{
foreach (UnicastIPAddressInformation ipInfo in nic.GetIPProperties().UnicastAddresses)
{
if (!ipInfo.Address.IsIPv6LinkLocal)
{
return ipInfo.Address;
}
}
}
}
}
return IPAddress.Any;
}