From: Martin Liddle on
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?
--
Martin Liddle, Tynemouth Computer Services, 3 Kentmere Way,
Staveley, Chesterfield, Derbyshire, S43 3TW.
Web site: <http://www.tynecomp.co.uk>.
From: Tony Mountifield on
In article <94mptFgF3bIIFwP1(a)tynecomp.invalid>,
Martin Liddle <News-reply_spam_a(a)tynecomp.co.uk> 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(a)softins.co.uk - http://www.softins.co.uk
Play: tony(a)mountifield.org - http://tony.mountifield.org
From: Martin Liddle on
In message <fvsger$j3f$1(a)softins.clara.co.uk>, Tony Mountifield
<tony(a)softins.clara.co.uk> writes
>
>I would be very surprised if it gethostbyname() that is causing the program
>to exit.
>
Agreed; it terminates slightly later.

>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.
>
Agreed and in my modified version this is exactly what I check.

>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. ???
>
The first description refers to how the program was; the second to my
modified version where it loops (the intention being it loops until it
succeeds with a 30 second sleep in each loop).
--
Martin Liddle, Tynemouth Computer Services, 3 Kentmere Way,
Staveley, Chesterfield, Derbyshire, S43 3TW.
Web site: <http://www.tynecomp.co.uk>.
From: Peter Benie on
In article <94mptFgF3bIIFwP1(a)tynecomp.invalid>,
Martin Liddle <News-reply_spam_a(a)tynecomp.co.uk> wrote:
>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.

Could this be negative caching by 'nscd'? Kill it and see if your
problems go away.

Peter
From: Martin Liddle on
In message <s3e*DFhcs(a)news.chiark.greenend.org.uk>, Peter Benie
<peterb(a)chiark.greenend.org.uk> writes
>Could this be negative caching by 'nscd'? Kill it and see if your
>problems go away.
>
Nice idea but nscd isn't running on this Fedora 8 system (although it is
installed). Is there anything else that could be doing something
similar?
--
Martin Liddle, Tynemouth Computer Services, 3 Kentmere Way,
Staveley, Chesterfield, Derbyshire, S43 3TW.
Web site: <http://www.tynecomp.co.uk>.
 |  Next  |  Last
Pages: 1 2
Prev: Are things getting difficult?
Next: Natting