This piece of code result in printing Error AHAHA:SUCCESS, and this also means that it run into else, which indicates he is NULL which shouldn't be.

UPDATE:

memset(&ipv4addr,0,sizeof(ipv4addr));

ipv4addr.sin_family = AF_INET;

adding these I somehow got the hostname of

ANantes-651-1-49-net.w2-0.abo.wanadoo.fr(2.0.0.0)???

网友答案:

As others have stated in comments, the gethostby...() functions assign the error code to h_errno rather than to errno. They are not the same thing! You can also use gethostbyaddr_r() to get the error code. But either way, use hstrerror() instead of strerror() to report the error message.

You are also not populating the sockaddr_in.sin_family field. That is why the code works with using inet_pton(). Even though gethostbyaddr() takes a family as an input parameter, you still have to pass it a correctly populated sockaddr_in so it can validate that you really are passing in an IPv4 address and not another address type, like IPv6.