Altera_Forum
Honored Contributor
20 years agoAssigning hostname and IP
Hi,
I'm attempting to use the gethostbyname() function to access the structure of type hostent (he) so I can print out the Hostname (he->h_name) and assigned IP address (he->h_addr_list). However, gethostbyname() returns a NULL indicating there is nothing in the returned "he" structure. Also, when I use the gethostname() function, the expected string "Nios2" hostname is returned.char hName;
struct hostent *he;
if (!(gethostname(hName, MAX_FILENAMELENGTH - 1)))
{
if ((he = gethostbyname(hName)) != NULL)
{
printf("\tHostname = %s. IP Address = %d.%d.%d.%d\n\n",
he->h_name, he->h_addr_list, he->h_addr_list,
he->h_addr_list, he->h_addr_list);
}
} Is there a setting I'm not aware of that needs to be initialized before the gethostbyname() function will return a non-NULL value? Does the hostname string (hName) need to be NULL (\0) terminated?