Reputation: 3440
I was looking up http://www.codeproject.com/KB/cs/network.aspx (How to get the IP address of a machine) and it mentions that "In the Win32 API this could be accomplished using the NetWork API."
I wish to use this API using C++ in order to find the IP address of a machine. I have the local computer's name on the network. I've tried looking it up on MSDN but only found the "Network Management" API and it doesn't appear to have the functions I need. I'm assuming I could use Win Socks to figure this out but it seems like a large amount of work for something which should be simple.
Any help appreciated.
EDIT: Should mention that I am talking about the local IP address, not external.
Upvotes: 0
Views: 6651
Reputation: 54148
You can do this locally using GetAdapterAddresses in the IP Helper API.
The GetAdaptersAddresses function retrieves the addresses associated with the adapters on the local computer.
Upvotes: 2
Reputation: 136391
you can use the gethostbyname
function
check this sample
#include <netdb.h>
#include <arpa/inet.h>
#include <iostream>
int main()
{
const char* const host = "thecomputername" ;
const hostent* host_info = 0 ;
host_info = gethostbyname(host) ;
if(host_info)
{
std::cout << "host: " << host_info->h_name << '\n' ;
for( int i=0 ; host_info->h_addr_list[i] ; ++i )
{
const in_addr* address = (in_addr*)host_info->h_addr_list[i] ;
std::cout << " address: " << inet_ntoa( *address ) << '\n' ;
}
}
else herror( "error" ) ;
}
Upvotes: 3