Re: Problem with gethostbyname



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
.



Relevant Pages

  • Re: DC Redundancy Not Working...?
    ... with no FAIL or WARN messages. ... Starting test: OutboundSecureChannels ... The connection was aborted by the remote WINS. ...
    (microsoft.public.windows.server.general)
  • Re: How can I tell when a remote TCP connection is closed?
    ... recv won't fail - it will return zero bytes read for graceful ... Microsoft MVP, MCSD ... >> I have a client with a TCP connection to a server. ... >> The remote end is doing a shutdown and close on the socket. ...
    (microsoft.public.win32.programmer.networks)
  • Re: connectivity redundancy setup question
    ... > The issue wasn't if the box fails just the connection. ... fail then a PC. ... >> internal interface and have the backup machine take it over ... box and connect both the broadband and POTS modems to it. ...
    (alt.os.linux)
  • Re: Broadband internet connection
    ... connection with the same UID and password - again it fails because of UID ... Pasting user ID's and passwords can sometimes fail. ... box, login would fail. ...
    (microsoft.public.windowsxp.network_web)
  • Problem with gethostbyname
    ... I am trying to update some code 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 then the program exits which is undesirable. ...
    (uk.comp.os.linux)