简体   繁体   English

获取物理IP地址的诀窍是什么?

[英]Whats the trick behind getting the physical IP address?

How can I get the same IP Address I get when I go to " http://www.whatsmyip.org/ " using C++ and winsock library? 使用C ++和Winsock库访问“ http://www.whatsmyip.org/ ”时,如何获得相同的IP地址?

I know how to get the "127.0.0.1" and the router IP "192.168.1.103"... But when I got to " http://www.whatsmyip.org/ " I get "65.55.105.132"... 我知道如何获取“ 127.0.0.1”和路由器IP“ 192.168.1.103” ...但是当我获取“ http://www.whatsmyip.org/ ”时,我会获取“ 65.55.105.132” ...

How can I accomplish this? 我该怎么做?

In a general way - you can't. 一般来说,您不能。 You could open an http connection to whatmyip and parse the result. 您可以打开与whatmyip的http连接并解析结果。 Or as Justin suggests in the comments, use http://www.whatismyip.com/automation/n09230945.asp and save on parsing and bandwidth. 或如Justin在评论中建议的那样,使用http://www.whatismyip.com/automation/n09230945.asp并节省解析和带宽。 But in the end, that's only going to give you the IP of the NAT / proxy / whatever that's between you and whatsmyip. 但是最后,这只会为您提供NAT /代理/与您和whatsmyip之间的任何内容的IP。 That address is only connected to your computer through forwarding. 该地址仅通过转发连接到您的计算机。

There isn't a general way to retrieve your router's internet facing IP address (that's the 65.55.105.132 IP you are seeing.) Even if you could, there's no reason that there couldn't be more layers of NATing getting in the way. 没有一种通用的方法来检索路由器的面向Internet的IP地址(即您正在看到的65.55.105.132 IP。)即使可以,也没有理由不会有更多的NAT层成为障碍。

You can't. 你不能

http://www.whatsmyip.org is telling you the IP address it sees when you talk to it. http://www.whatsmyip.org告诉您与之交谈时看到的IP地址。 It could be seeing yours, or a proxy server, or a NAT box, or anything else. 可能看到的是您的,代理服务器,NAT盒或其他任何东西。 There's no way for you to know. 您无法知道。

What are you trying to accomplish? 你想达到什么目的?

There is no way to accomplish this without using something that is outside of your network. 如果不使用网络外部的东西,就无法实现此目的。

The address returned is the first public ip address between your computer and the outside world. 返回的地址是您的计算机与外界之间的第一个公共IP地址。 10. , 192.168. 10. ,192.168。 , 172.[16-31].* ip address are "private" and should not be forwarded intact to any non-private ip address. ,172. [16-31]。* IP地址是“私有”的,不应原样转发到任何非私有IP地址。

At some point, private ip addresses must be linked to a public ip address. 在某些时候,私有IP地址必须链接到公共IP地址。 The first public ip address in the path from your computer to the whatsmyip.com site is what is displayed on that page. 从您的计算机到whatsmyip.com网站的路径中的第一个公共ip地址是该页面上显示的内容。 The trick is that your computer might have a public ip, or there might be some number of hops to computers/switches/routers that have private ip's until there is eventually a public ip. 诀窍是您的计算机可能有一个公共ip,或者可能会有一些跳到具有私有ip的计算机/交换机/路由器,直到最终有一个公共ip。 There is no way to know what that number of hops is, and depending on the network the route can change from one request to another and there migth be a different number of hops each time (unlikely for a home network, but more likely in a corporate network). 无法知道跳数是多少,并且路由可能会根据网络从一个请求更改为另一个请求,并且每次迁移的跳数都不同(对于家庭网络而言不太可能,但是在一个网络中更可能企业网络)。

The only way to get the first public ip address is to send a packet to an address that is public, and trace the route that it took. 获取第一个公共ip地址的唯一方法是将数据包发送到公共地址,并跟踪其路由。 Again...that route can (and does) change on a complex network. 同样,该路由可以(并且确实)在复杂的网络上更改。

If you have a specific problem that you are trying to solve, there might be a better answer. 如果您要解决特定的问题,可能会有更好的答案。

As others have said, you need a third party on the network such as whatismyip.com to tell you your IP address. 正如其他人所说,您需要网络上的第三方(例如whatismyip.com)来告诉您您的IP地址。 I just wanted to add, you shouldn't depend on whatismyip.com, instead you should consider setting up your own, as services like this tend to come and go or change who may connect to them (see this example for why that may happen). 我只是想补充一点,您不应该依赖whatismyip.com,而应该考虑建立自己的服务,因为这样的服务会来去去去或更改可能与他们连接的人(请参阅此示例,了解为什么会发生这种情况) )。

You can easily set up a service to do this just for your application for free, say, based on Google AppEngine. 您可以轻松地设置一项服务,例如仅基于Google AppEngine,免费为您的应用程序执行此操作。 The requisite request handler might look something like: 必需的请求处理程序可能类似于:

from google.appengine.ext import webapp

class RemoteAddressHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write(self.request.remote_addr)

Well, there is no trick really. 好吧,真的没有技巧。 The reason www.whatsmyip.com can tell your "outside" IP address is because they receive a request that has been NATed (Network Address Translated) by your router and so they see your public IP address. www.whatsmyip.com可以告诉您“外部” IP地址的原因是,因为它们收到路由器发送的NAT(网络地址转换)请求,因此他们可以看到您的公共IP地址。 That is something that you can't really get at an arbitrary internal host on your network - not unless you have a server on the outside that you could query or have a mechanism to ask your router what IP address it uses when making public internet requests.. 那是您无法真正在网络上的任意内部主机上获得的东西-除非您在外部可以查询服务器或拥有一种机制来询问路由器在发出公共Internet请求时使用的IP地址的机制..

As others have said, there may not be an easy / great solution. 正如其他人所说,可能没有一个简单/好的解决方案。 An (albeit hacky) solution might be to shell out to the 'tracert' system call. 一个(尽管很hacky)的解决方案可能是将其封装为“ tracert”系统调用。 Ex: 例如:

tracert google.com

This call will return a list of all routers in between your PC and google.com (or whatever external site.) You can then iterate through the results, taking the first node that isn't a private IP address range. 此调用将返回您的PC和google.com(或任何外部站点)之间的所有路由器的列表。然后,您可以遍历结果,并获取非私有IP地址范围内的第一个节点。 (Ie, not 192.168. . , etc.) (即,不192.168。,等)

Granted, this is by no means an elegant solution. 当然,这绝不是一个优雅的解决方案。 However, it might give you the info you need. 但是,它可能会为您提供所需的信息。 As a test, you can simply run the above from a command line and see if it's helpful. 作为测试,您可以从命令行简单地运行以上命令,看看是否有帮助。

Wow that was a very quick reply. 哇,这是一个非常快速的回复。 Thanks guys for the answers now I know better. 谢谢你们的答案,现在我知道了。

I am currently building a C++ HTTP server for learning purpose. 我目前正在建立一个用于学习目的的C ++ HTTP服务器。 I accomplish to forward the IP address I get when I got to "whatismyip.org" to my private ip "192.168.1.3" making any external call to be handle by my server. 我完成了将到达“ whatismyip.org”时获得的IP地址转发到我的私有IP“ 192.168.1.3”的操作,以进行服务器进行的任何外部呼叫。 So pretty much I created a simple C++ server. 差不多我创建了一个简单的C ++服务器。 However I am using something called No-IP DUC to translate "yellowyackets.no-ip.org" to my public IP so that people don't have to memorize my public IP. 但是,我正在使用一种称为No-IP DUC的东西将“ yellowyackets.no-ip.org”转换为我的公共IP,这样人们就不必记住我的公共IP。 But because the IP changes I have to keep track to whats is my new IP so that people can keep connecting to me... so I have to go to "whatsmyip.org" to know my new IP. 但是由于IP更改,我必须跟踪什么是我的新IP,以便人们可以继续与我保持联系...所以我必须去“ whatsmyip.org”了解我的新IP。 Then I wanted to create a program that would actually tell me thats my new IP. 然后,我想创建一个程序,该程序实际上会告诉我这是我的新IP。

I dunno if what I just said make sense but... I am brand new to servers... Like I said this is for learning purpose. 我不知道我刚才说的话是否有意义,但是...我是服务器的新手...就像我说的这是出于学习目的。

Thanks again. 再次感谢。 and good luck to everyone. 祝大家好运

在此处输入图片说明

One c/c++ way of getting the external ip is to use a web based IP address API tool, download the webpage containing your IP address into a char array and extract the IP address from the HTML source. 获取外部ip的一种c / c ++方法是使用基于Web的IP地址API工具,将包含IP地址的网页下载到char数组中,然后从HTML源中提取IP地址。 Here is some winsock code to demonstrate it. 这是一些winsock代码来演示它。 it uses http://api.ipify.org/ 's online web api. 它使用http://api.ipify.org/的在线网络api。

//
// Winsock get external ip address from website api at   api.ipify.org
// api.ipify.org
//

#include <string.h>
#include <stdio.h>

#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <vector>

#include <algorithm>
#include <cctype>
#include <locale>
#include <fstream>
#include <ctime>
#include <cstdlib>

using namespace std;
#pragma comment(lib,"ws2_32.lib")


string website_HTML;
locale local;
char ipaddress[16];
int ic=0;
void get_Website(char *url );
char mystring[] = " ";
char seps[]   = " ,\t\n";
char *token;
char lineBuffer[200][80] ={' '};
char buffer[10000];
char ip_address[16];
int i = 0, bufLen=0, j=0,lineCount=0;
int lineIndex=0, posIndex=0;


int main( void ){

    SYSTEMTIME st;
    GetLocalTime(&st);
    char *today = new char[32];
    memset(today,' ', sizeof(today) );
    sprintf(today,"%d-%d-%d", st.wYear , st.wMonth , st.wDay);
    memset(buffer,'\0',sizeof(buffer));

    get_Website("api.ipify.org" );
    for (size_t i=0; i<website_HTML.length(); ++i) website_HTML[i]= tolower(website_HTML[i],local);

    token = strtok( buffer , seps );  
    while( token != NULL ){

      strcpy(lineBuffer[lineIndex],token);
      int dot=0;
      for (int ii=0; ii< strlen( lineBuffer[lineIndex] ); ii++ ){

          if (lineBuffer[lineIndex][ii] == '.') dot++;
          if (dot>=3){
              dot=0;
              strcpy(ip_address,lineBuffer[lineIndex]);
          }
      }

      token = strtok( NULL, seps );       
      lineIndex++;
   }
     cout<<"Your IP Address is  "<< ip_address<<" \n\n";

 return 0;
}


void get_Website(char *url ){
    WSADATA wsaData;
    SOCKET Socket;
    SOCKADDR_IN SockAddr;
    int lineCount=0;
    int rowCount=0;
    struct hostent *host;
    char *get_http= new char[256];

    memset(get_http,' ', sizeof(get_http) );
    strcpy(get_http,"GET / HTTP/1.1\r\nHost: ");
    strcat(get_http,url);
    strcat(get_http,"\r\nConnection: close\r\n\r\n");

    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0){
        cout << "WSAStartup failed.\n";
        system("pause");
        //return 1;
    }

    Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    host = gethostbyname(url);

    SockAddr.sin_port=htons(80);
    SockAddr.sin_family=AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);

    if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
        cout << "Could not connect";
        system("pause");
        //return 1;
    }
    send(Socket,get_http, strlen(get_http),0 );

    int nDataLength;
    while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){        
        int i = 0;
        while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r'){

            website_HTML+=buffer[i];
            i += 1;
        }               
    }

    closesocket(Socket);
    WSACleanup();
    delete[] get_http;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM