Jump to content

Resolve Hostname to IP with timeout


Recommended Posts

Does anyone know of an easy way to resolve a hostname to IP Address with the ability to time out?

GetHostEntry just takes too long if it can't resolve, I've tried setting up a function similar to this using the Asynchronus version that would start lookup wait a certian amount of milliseconds then end though i dont really understand how it works properly so that attempt failed.

Thanks

Link to comment
Share on other sites


Visual C++ 2008 (i put it in the subtopic)

		bool pingTargetHost(System::String^ stringHostName)  {
//try {
// IPHostEntry^ host = Dns::GetHostEntry(stringHostName);
//}
//catch (Exception^ exceptionResolve) {
// return false;
//}
Ping ^ pingSender = gcnew Ping;
int timeout = 5;
PingReply ^ reply;

try {
reply = pingSender->Send(stringHostName, timeout);
}
catch (Exception^ exceptionPING) {
return false;
}

if (reply->Status == IPStatus::Success) {
return true;

} else {
return false;
}
}

The commented out bit i want to check for dns resolve first and have a timeout of like 5 milliseconds, i had deleted my earlier attempt at asynchronous version.

Want to check that the hostname resolves first so that the ping doesn't have the same timeout problem in the event it can resolve.

Link to comment
Share on other sites

trying to use asynchronus method again but cannot find a way of getting the status of the async attempt once it has began, so that if it hasn't resolved after sleep is finished it returns false and stops the attempt.

		bool pingTargetHost(System::String^ stringHostName)  {

System::AsyncCallback^ callback = gcnew AsyncCallback(ProcessDnsResults);
System::Object^ state;
Dns::BeginGetHostEntry(stringHostName, callback, state);

System::Threading::Thread::Sleep(5);

//ResolveState^ ioContext = (ResolveState^)( callback->AsyncState );
//ioContext->IPs = Dns::EndGetHostEntry( ar )
//System::IAsyncResult^ testresult = callback;
//Dns::EndGetHostEntry(testresult);

if (testresult->IsCompleted == false) {
return false;
}

Ping ^ pingSender = gcnew Ping;
int timeout = 5;
PingReply ^ reply;

try {
reply = pingSender->Send(stringHostName, timeout);
}
catch (Exception^ exceptionPING) {
String^ stringException = exceptionPING->Message;
return false;
}

if (reply->Status == IPStatus::Success) {
return true;

} else {
return false;
}
}

Link to comment
Share on other sites

5ms is no where near enough time to (reliably) do name lookups. Why are you doing it repeatedly? Even if the response times were ideal, it's still going to badly skew your ping times. Do it once, then pass the result to the ping code. ...Then the 2sec you should wait for a reliable answer won't be such a big deal.

Just a Thought

Stoic Joker

Link to comment
Share on other sites

5ms seems to be plenty of time when i ping any hostname on the lan and that hostname is valid and the computer accepts icmp echo requests then the reply is obtained usualy beteen 2 and 4 ms, this function will only be used on lan side

the reason i'm doing a dns resolve before the ping is so i can see if the dns will resolve if it doesnt withing 5ms or possibly slightly longer delay then return false else let the code continue so it can ping.

it's the dns resolve that takes ages if the dns hostname is invalid, which i want to get around by using async then after 5ms see if it has finished if not then return false

Link to comment
Share on other sites

(5ms) Might seem like enough time, but it ain't ... especially if the device you're pinging has a sleep mode (like a printer). I've seen them take up to 150ms to answer the first ping (depending on how soundly they were "sleeping").

Mind you I only have the snippet posted to try to grasp the intent with. But a typical ping would go:

Name Lookup

ping

ping

ping

ping

It appears you are going:

Lookup

ping

Lookup

ping

Lookup

ping

Lookup

ping

Which granted would make the standard 3sec timeout rather annoying as you loop past it four time.

I generally only code in pure Win32 API C++ so I usually just use gethostbyname(pstrHost); which I've never had a problem with.

If you are trying to force your way past a standard protocol timeout, you're pretty much asking for trouble. Better would be to rethink the overall approach. May multi thread at a higher point so multiple targets can timeout simultaneously.

Link to comment
Share on other sites

the program i am making is basically an app that is an overhead map of the room layout with all the computers listed on it when you move the mouse over a button that corresponds to the pc it pings the pc to see if its on or off, if it is off then it doesn't do anything if it is on then it shows a context menu of options.

the function works fine if the dns server has that hostname setup however if that hostname is not on the pc then it takes an unacceptable amount of time to complete, the ping timeout doesnt appear to effect dns response and so thats why i added the dns resolve code at the start to try and find a method around that.

i just tried using the winapi stuff using gethostbyname however thats not really going to work for me it seems as it has problems when it comes to std::strings and System::Strings i'm getting conflicts i cant fix as it wants to convert one to the other or vice versa and fails.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...