Frage

I have this code:

struct hostent *host;
struct hostent *serverHost;
if ((host = gethostbyname(param.ipadress)) == NULL)
{
  return -1;
}
if ((serverHost = gethostbyname(param.dnsserver)) == NULL)
{
  return -1;
}

and now when I tried host->h_addr it contains address from param.dnsserver. What is wrong? What I am doing wrong?

War es hilfreich?

Lösung

gethostbyname and gethostbyaddr are old functions that should not be used anymore.

For their POSIX (2004) description:

The gethostbyaddr() and gethostbyname() functions may return pointers to static data, which may be overwritten by subsequent calls to any of these functions.

Which means you need to copy the parts you want from the return value before a subsequent call.

Additionally:

The gethostbyaddr() and gethostbyname() functions may be withdrawn in a future version.

And indeed, they are no longer in POSIX 2008.

The replacements are:

The getaddrinfo() and getnameinfo() functions are preferred over the gethostbyaddr() and gethostbyname() functions.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top