Solved

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

Posted on 2002-07-09
2
208 Views
Last Modified: 2010-04-22
How can we get the IP of my computer using C program?
Please tell me the sample code by C.
0
Comment
Question by:mathematics
2 Comments
 
LVL 10

Accepted Solution

by:
makerp earned 50 total points
ID: 7140673
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
 

Author Comment

by:mathematics
ID: 7142237
Thank you very much,makerp.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question