Archived

This topic is now archived and is closed to further replies.

jonbell

Local IP's

Recommended Posts

jonbell    100
How can i enumerate all the IP''s on a local machine? I am using sockets and getsockname but it returns 0.0.0.0 for the server as it is listening on all ip''s but i want to list all the ip''s.

Share this post


Link to post
Share on other sites
MauMan    344
look at gethostbyname(). Here''s a fragment that''s putting them into a tooltip


//

// List Name/IP addresses for this server

//

const int cnHostname = 1024; // should be plenty big

char szHostname[cnHostname];
string sTooltip;
gethostname( szHostname, cnHostname );
struct hostent* pHostEnt = gethostbyname( szHostname );

//

// The address(es) are in an array that is null terminated

//

int i = 0;
struct in_addr** ppAddr = 0;
for ( ppAddr = (struct in_addr**)pHostEnt->h_addr_list; *ppAddr; ppAddr++ )
{
if ( i++ != 0 ) // on second and later...

sTooltip += "\n";
sTooltip += string(inet_ntoa( **ppAddr ));
}

Share this post


Link to post
Share on other sites