Aayman Khalid
Aayman Khalid

Reputation: 468

gethostname() in windows

I've found out that the gethostname() function returns the standard host name for the local computer, but I am a little confused about the term "host name",is it the name of the computer at which the function is being called or the name of the server with which the computer is connected in a network...

          char szPath[128] = "";
          gethostname(szPath, sizeof(szPath));

also what shoud I do if I want to find out the ip address of the local host...

Upvotes: 1

Views: 16493

Answers (2)

Arun
Arun

Reputation: 2092

gethostname : returns local computer name.

To get IP address, use:

   struct hostent *ent = gethostbyname(hostname);
   struct in_addr ip_addr = *(struct in_addr *)(ent->h_addr);
   printf("Hostname: %s, was resolved to: %s\n",
           hostname, inet_ntoa(ip_addr));     

Upvotes: 4

NPE
NPE

Reputation: 500157

From the documentation:

The gethostname function retrieves the standard host name for the local computer.

Upvotes: 3

Related Questions