How can we get the ip of my computer using C program?

How can we get the IP of my computer using C program?
Please tell me the sample code by C.
mathematicsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
makerpConnect With a Mentor Commented:
you will need to include the socket headers and link to the sockets library, this uses stl, so include <string> aswell

string get_ip_address_from_host_name(string host_name)
{
     string retval;
     struct hostent *host;
     struct sockaddr_in addr;
     if((host = gethostbyname(host_name.c_str())) != NULL)          
     {
          memcpy((char*)&addr.sin_addr,host->h_addr,host->h_length);
          retval = inet_ntoa(addr.sin_addr);
     }
     return retval;
}
0
 
mathematicsAuthor Commented:
Thank you very much,makerp.
0
All Courses

From novice to tech pro — start learning today.