Re: Problem with gethostbyname
- From: tony@xxxxxxxxxxxxxxxxxxx (Tony Mountifield)
- Date: Wed, 7 May 2008 15:04:59 +0000 (UTC)
In article <94mptFgF3bIIFwP1@xxxxxxxxxxxxxxxx>,
Martin Liddle <News-reply_spam_a@xxxxxxxxxxxxxx> wrote:
I am trying to update some code (that I didn't originally write) which
uses gethostbyname to turn a name into an IPv4 address. This works well
enough most of the time but if the internet connection fails (which it
does from time to time because it might be dialup, via mobile phone or
WiFi) then the program exits which is undesirable. I was hoping that I
could insert a loop so that instead of exiting the program sleeps for a
while and then tries again until the connection is available again. This
doesn't work; once a call has failed all subsequent calls fail until the
program is restarted. I am not running a local DNS server; resolv.conf
just points at the nameservers provided by whatever connection happens
to be in use. Any clue what I am doing wrong or how this should be
coded?
I would be very surprised if it gethostbyname() that is causing the program
to exit.
If gethostbyname() encounters an error, it returns NULL instead of a
pointer to a struct hostent. You need to make sure you check that the
return value is non-NULL before you try to dereference it, or your program
will indeed exit.
Actually, re-reading your description now confuses me: first you say "the
program exits", but later you say "all subsequent calls fail until the
program is restarted", which suggests it is still running. ???
Could you describe the relevant code in more detail or even include
a listing of the relevant section?
Cheers
Tony
--
Tony Mountifield
Work: tony@xxxxxxxxxxxxx - http://www.softins.co.uk
Play: tony@xxxxxxxxxxxxxxx - http://tony.mountifield.org
.
- Follow-Ups:
- Re: Problem with gethostbyname
- From: Unruh
- Re: Problem with gethostbyname
- From: Martin Liddle
- Re: Problem with gethostbyname
- References:
- Problem with gethostbyname
- From: Martin Liddle
- Problem with gethostbyname
- Prev by Date: Problem with gethostbyname
- Next by Date: Re: Removing/replacing open files
- Previous by thread: Problem with gethostbyname
- Next by thread: Re: Problem with gethostbyname
- Index(es):
Relevant Pages
|