Solved

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

Posted on 2002-07-09
2
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 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