I'm trying to get IP address of a dns server with the code:
var address = Dns.GetHostAddresses(domain)[0];
Its working on my pc, but when I'm doing it through someone else pc its giving him different result (IPv6 instead of IPv4). I did try to use the method
var ipV4Address = address.MapToIPv4();
but that just returning the wrong IP address
p.s: btw, when using the command ping dns.address.blabla on both computers the result is the same.
If the domain have IPv4 and IPv6 addresses, you'll get both. Filter by AddressFamily to InterNetwork only (IPv4) instead of InterNetworkV6 (IPv6)
var ipV4Address = Dns.GetHostAddresses(domain)
.First(a => a.AddressFamily == AddressFamily.InterNetwork);
To answer your specific question in the header: Yes, it is. Here's an outline of how to do that:
using System.Net.NetworkInformation;
...
private static bool PingWithResponse(int timeout, string iP, out string response)
{
bool result = false;
using (var p = new Ping())
{
var r = p.Send(iP, timeout);
response = $"Ping to {iP} [{r.Address}]";
if (r.Status == IPStatus.Success)
{
response += $" Successful Response delay = {r.RoundtripTime} ms";
result = true;
}
else
{
response += $" Failed Status: {r.Status}";
}
}
return result;
}