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

  • Doesn't work

    Posted by Legacy on 12/04/2002 12:00am

    Originally posted by: Patrick

    Now, can you please help me how to display the IP with printf ?

    Reply
  • You need #include <winsock2.h> and link Ws2_32.lib

    Posted by Legacy on 11/07/2002 12:00am

    Originally posted by: Chris

    He forget to mention add:

    #include <winsock2.h>

    and also click Project->Settings->Link Tab
    and add in:

    Ws2_32.lib

    Reply
  • It doesn't work....

    Posted by Legacy on 10/08/2002 12:00am

    Originally posted by: Omar Mukhtar

    The program listed here claims to get IP local address & local host name. But it didn't work well in Linux environment.

    It retrieves "127.0.0.1" i.e. Loopback IP address. The reason behind is that gethostbyname() function looks for IP in a file. What is the real solution?

    How does ifconfig command works?????

    Omar Mukhtar

    Reply
  • serial port programming

    Posted by Legacy on 07/27/2002 12:00am

    Originally posted by: deepaa.

    How to communicate with two adjacent computers.how to communicate thru the serial ports?

    Reply
  • Re: Obtaining IP address

    Posted by Legacy on 03/14/2002 12:00am

    Originally posted by: John Payne

    For the above code to work, you need to include the header file:

    #include <unistd.h>

    Reply
  • How do I get all the IP addresses on a LAN having a WINDOWS NT 4 SERVER.

    Posted by Legacy on 11/04/2001 12:00am

    Originally posted by: Ajay

    How do I get all the IP addresses on a LAN having a WINDOWS NT 4 SERVER.

    Reply
  • Its Ok, How can I get IPv6 host address in the same manner.

    Posted by Legacy on 10/15/2001 12:00am

    Originally posted by: Mura


    Your example gives the shortest way.
    It works fine for IPv4. But I am trying
    to get the IPv6 address using WSAIoctl(..) :(

    Give some example

    Reply
  • How to get IP in Linux

    Posted by Legacy on 11/10/2000 12:00am

    Originally posted by: Senthilkumaran

    I want to get the IP address and Host name of a computer running Linux. Please help me out.

    Thanks,

    Reply
  • Thanks for the addition.

    Posted by Legacy on 01/14/1999 12:00am

    Originally posted by: Jeff Lundgren

    I've had a lot of people ask this question.

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

Top White Papers and Webcasts

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds