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

  • On-demand Event Event Date: September 23, 2015 The cloud is not just about a runtime platform for your projects – now, you can do your development in the cloud, too. Check out this webcast to learn how the cloud improves your development experience and team collaboration. Join Dana Singleterry, Principal Product Manager for Oracle Dev Tools, as he discusses how to simplify every aspect of the development lifecycle, including requirements gathering, version management, code reviews, build automation, and …

  • Thanks to the Internet of Things (IoT), physical assets are turning into participants in real-time global digital markets. The countless types of assets around us will become as easily indexed, searched and traded as any online commodity. While some industries will be tougher to transform than others – those with physical limitations, such as manufacturing, will be harder to digitize – untold economic opportunities exist for growth and advancement. Our research shows this will create a new "Economy …

Most Popular Programming Stories

More for Developers

RSS Feeds

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