Hello everyone.
I want to know how to get my internet IP without going to a "www.ip-address.com" or somthing to get it. I thought about using winsock, but i am still a winsock beginner. I want to connect to 127.0.0.1 (loopback ip) and do a GetHostInfo() command of some kind to get the ip. The ip i want is not the same ip you can get on CMD. I want the "internet IP", the one that you would use for another client to connect to.
Thanks.

Recommended Answers

All 4 Replies

To my knowledge this isn't plausible. This is why: you have an internal IP address, on your computer, probably something like 192.168.1.101. Loopback (127.0.0.1) is an entirely different interface... so, let's just say that your connection to the internet is through Ethernet 0 (eth0). eth0, basically asks the router for an IP address, which gets assigned to it (192.168.1.101). Your router, when it gets turned on and the internet plugged into it, asks your ISP for an IP address. So, router asks ISP for address (the address YOU want), which gets assigned. Then your computer (presumably the one that would run your code) asks the router for an IP. Those IP's are very different networks. So, I guess it looks something like:

_____________                            192.168.1.1  _______     98.65.128.12               ____ 
| yourcomputer   |---------------------------------- | router  |----------------------------|  ISP  |
| and your code  |  192.168.1.101                   ------------                            --------
--------------------

so tell me..... how does "your code" all the way on the left, on a completely different network, on the "other side" of the router, get the address of the router on the inaccessible (right side) of the router WITHOUT going to something like www.ip-address.com" ?

Edit: sorry, just realize the drawing gets messed up on different resolutions

Well thanks man, i guess it cant be done. But thanks anyway.
So there is no possible way to get the ip? i found a few things with java but i dont know if they will work because i havent figured out how to compile with java : /

Also... When you ping google.com it returns back and it shows googles ip [209.85.171.100] is there a way to like ping your self and get your ip? I have a no-ip, i pinged that and guess what. I got my internet ip.
But how would i be able to like ping a loopback adress and get it? Not possible?
But when i ping 127.0.0.1 i get in return the same adress no ip. and when i ping loopback, i get ::1 ?? wtf?

Below is some code I got from CodeGuru

#include <iostream>
#include <string>
#include <winsock2.h>
using namespace std;
#pragma warning(disable: 4996)

string hostn()
{
char szHostName[128];
std::string str;

if( gethostname(szHostName, sizeof(szHostName)) == 0 )
{
	// Get host adresses
	struct hostent * pHost;
	int i;

	pHost = gethostbyname(szHostName);

	for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
 	{
 		int j;

 		for( j = 0; j < pHost->h_length; j++ )
 		{
 			if( j > 0 )
 				str += ".";
            char addr[18];
 			sprintf(addr,"%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)

 	}
}
    return str;
}


int main()
{
    char name[255] = {0};
    WSADATA wsaData;
    WORD version;
    int error;

    version = MAKEWORD( 2, 0 );

    error = WSAStartup( version, &wsaData );

    std::string s = hostn();

    cout << s << "\n";
    WSACleanup();
}

Below is some code I got from CodeGuru

#include <iostream>
#include <string>
#include <winsock2.h>
using namespace std;
#pragma warning(disable: 4996)

string hostn()
{
char szHostName[128];
std::string str;

if( gethostname(szHostName, sizeof(szHostName)) == 0 )
{
	// Get host adresses
	struct hostent * pHost;
	int i;

	pHost = gethostbyname(szHostName);

	for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
 	{
 		int j;

 		for( j = 0; j < pHost->h_length; j++ )
 		{
 			if( j > 0 )
 				str += ".";
            char addr[18];
 			sprintf(addr,"%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)

 	}
}
    return str;
}


int main()
{
    char name[255] = {0};
    WSADATA wsaData;
    WORD version;
    int error;

    version = MAKEWORD( 2, 0 );

    error = WSAStartup( version, &wsaData );

    std::string s = hostn();

    cout << s << "\n";
    WSACleanup();
}

tyvm.
Only problem is, i need the internet IP. That gives the local ip.
Nice try tho : /

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.