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
			str += addr;
  		// str now contains one local IP address - do whatever you want to do with it (probably add it to a list)


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

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date