Get hostname and ip address of local computer(2)

Following is a code that gets local machine IP addresses. The advantages over the article by Jeff Lundgren are that my code recognises ALL IP addresses and is ready for IPv6 ;-)
char szHostName[128];

if( gethostname(szHostName, 128) == 0 )
{
	// Get host adresses
	struct hostent * pHost;
	int i;
 
	pHost = gethostbyname(szHostName);
 
	for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
 	{
		 CString str;
 		int j;
 
 		for( j = 0; j < pHost->h_length; j++ )
 		{
			 CString addr;
 
 			if( j > 0 )
 				str += ".";
 
 			addr.Format("%u", (unsigned int)((unsigned
		 	char*)pHost->h_addr_list[i])[j]);
			str += addr;
 		}
  		// str now contains one local IP address - do whatever you want to do with it (probably add it to a list)
 	}
}




Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds