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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds