문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top