CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2007
    Posts
    119

    Question API to find IP Address of current Computer.

    Hi,

    Is there any API through which i can find the IP address of current computer(computer on which my exe is running).

    Warm Regards,
    Mushq

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

  3. #3
    Join Date
    Sep 2004
    Posts
    1,361

    Re: API to find IP Address of current Computer.

    The above method works well on a card with a single network adapter (or single live network adapter) but may return the incorrect address on multi-honed machines.

    The following is a function I wrote which will give you the right IP address reguardless of how many nics a machine has.

    Code:
    bool GetIP(CString &MyIP)
    {
    	SOCKADDR_IN stLclAddr;
    	SOCKADDR_IN stRmtAddr;
    	int nAddrSize = sizeof(SOCKADDR);
    	SOCKET hSock;
    	int nRet;
    
    	//////////////////////////////////////////////////////////////////////////////
    	// I get the address by creating an UDP socket to some arbitrary address.
    	// I cannot use gethostname and then gethostbyname because that will return
    	// a list of all ip address on the system, including for example IEEE1394
    	// adapters that aren't even connected to the network but that have an
    	// address.
    	//////////////////////////////////////////////////////////////////////////////
    
    	// Init local address (to zero)
    	stLclAddr.sin_addr.s_addr = INADDR_ANY;
    	// Get a UDP socket
    	hSock = socket(AF_INET, SOCK_DGRAM, 0);
    	if (hSock != INVALID_SOCKET)
    	{
    		// Connect to arbitrary port and address (NOT loopback)
    		stRmtAddr.sin_family = AF_INET;
    		stRmtAddr.sin_port   = htons(IPPORT_ECHO);
    		stRmtAddr.sin_addr.s_addr = inet_addr("209.85.165.104");
    		nRet = connect(hSock, (LPSOCKADDR)&stRmtAddr,
    			sizeof(SOCKADDR));
    		if (nRet != SOCKET_ERROR)
    		{
    			// Get local address
    			getsockname(hSock, (LPSOCKADDR)&stLclAddr, (int
    				FAR*)&nAddrSize);
    		}
    		closesocket(hSock);
    	}
    
    	unsigned long IP = stLclAddr.sin_addr.s_addr;
    
    	// Only fill this out if we have a non-zero IP, we want an empty string for zero IP addresses
    	if ( 0 != IP )
    	{
    		unsigned char A, B, C, D;
    
    		A = (char)(IP & 255);
    		B = (char)(IP >> 8) & 255;
    		C = (char)(IP >> 16) & 255;
    		D = (char)(IP >> 24) & 255;
    
    
    		MyIP.Format("%d.%d.%d.%d", A, B, C, D);
    		return true;
    	}
    
    	return false;
    
    }
    It does assume that you have setup the sockets sub-system first such as WSAStartup() or you chose to enable windows sockets in the app-wizard when you created your application.

    This will generate the IP of the actual nick that packets will go out of. This routine doesn't actually send packets out so you do not need to worry about generating traffic with it or packet storming google.
    Last edited by DeepT; May 21st, 2007 at 10:22 AM.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: API to find IP Address of current Computer.

    Where does this address ("209.85.165.104") come from?

  5. #5
    Join Date
    Sep 2004
    Posts
    1,361

    Re: API to find IP Address of current Computer.

    It is google. It doesn't matter what address it is as long as it is routable. I had a comment to that effect, but when I edited my post I deleted it and didn't notice.

    Also, the address does not need to be reachable, just "theoretically" routeable from your machine's point of view.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: API to find IP Address of current Computer.

    Quote Originally Posted by DeepT
    It is google. It doesn't matter what address it is as long as it is routable.
    If it does NOT matter then why not to use INADDR_BROADCAST instead?

  7. #7
    Join Date
    Sep 2004
    Posts
    1,361

    Re: API to find IP Address of current Computer.

    Try it and see what happens. Maybe it will work fine, although I am not sure that 255.255.255.255 is routable.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: API to find IP Address of current Computer.

    I tried and it worked at least in one test (the same I have to say about 209.85.165.104 address - in one test it worked!).
    Are you sure your code with the 209.85.165.104 address will work always?

  9. #9
    Join Date
    Sep 2004
    Posts
    1,361

    Re: API to find IP Address of current Computer.

    It will always work as long as the address is routable. This implementation also works on any platform (thought there might need to be tweaks for small syntax changes). I use it on Windows, Linux, and Mac OSX.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured