开发者

How do I test my DNS connection?

开发者 https://www.devze.com 2022-12-15 07:34 出处:网络
A simple question. How do I test if my current DNS is working or down? Would it involve polling? That I fear would take up resources and also开发者_JAVA百科 require execution on a separate thread.Yes

A simple question. How do I test if my current DNS is working or down? Would it involve polling? That I fear would take up resources and also开发者_JAVA百科 require execution on a separate thread.


Yes it would involve looking up something, resolving a host name or some other DNS function. In addition you'd need to ensure that the host you're trying to resolve has not been cached in the local DNS cache too.

The Dns.GetHostByName function performs a lookup, but that doesn't mention if it will use the local cache. It's likely that it does, as it probably uses the OS lookup functionality under the hood.


If you're trying to do this in C# code check out the

namespace System.Net

documentation found here: http://msdn.microsoft.com/en-us/library/system.net.aspx

...which includes the class...

public static class Dns

documentation found here: http://msdn.microsoft.com/en-us/library/system.net.dns.aspx

which will allow your to write code such as:

IPHostEntry hostInfo = Dns.GetHostByName("www.contoso.com");

There is additional functionality in the Dns class that may be of use to you depending on what you're trying to accomplish.

This doesn't provide a way to check if your "DNS is down" directly but one would assume if you can't resolve a website that should always be up (e.g. www.microsoft.com or www.google.com) then your dns is down or you have lost your connection to the internet. If this isn't what you're looking for please comment up a little more detail and we'll see if we can help.


try to resolve a domain name using dns.


Several things:

Polling 1:

Because of the possibility to get back bached values you probably want to look up non-existent random domains (guid.somedomainyoucontrol.com maybe) and check if you get a timeout or an expected NXDOMAIN result.

Polling 2:

Regarding your fear that this might cost a lot of resources: You really have to explain what you want to do here for a real answer, but: DNS is a very lightweight protocol. I don't think that it takes a huge hit on your client or server, depending on the polling interval. Heck, there are even "TCP over DNS" tunnels out there, that really stress what DNS can do and they don't usually destroy the network for others either or DOS the server. Probably the best thing would be to settle on a conservative polling timeout and resolving a subdomain below some domain you own, probably even directly querying a nameserver that you control.

"Connection":

DNS uses UDP, most of the time. It can switch to or be used via TCP, but that's not the "normal" usage. So the term "connection" is probably misleading. You can just test for any point in time if you can resolve hosts right now.

0

精彩评论

暂无评论...
验证码 换一张
取 消