客户端的外部IP地址 [英] External IP Address of the client

查看:171
本文介绍了客户端的外部IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

听起来很有趣,但我如何从客户端获取外部IP地址?

Sounds funny, but how can I get the external IP address from a client?

我尝试了一些东西,但对我没用。

I tried few things, but didn't work for me.

我尝试的第一个地方

request.getRemoteAddr()

我得到的结果为:0:0:0:0:0:0:0:1

and I am getting the result as: 0:0:0:0:0:0:0:1

我尝试了第二名

InetAddress ip = InetAddress.getLocalHost();
ip.getHostAddress());

我得到的结果为:127.0.0.1

and I am getting the result as: 127.0.0.1

排在第三位我试过了

        URL whatismyip = new URL("http://checkip.dyndns.org:8245/");
        BufferedReader inIP = new BufferedReader(new InputStreamReader(whatismyip.openStream()));

        String IPStrOld = inIP.readLine(); //IP as a String
        String IPStrNewest = IPStrOld.replace("<html><head><title>Current IP Check</title></head><body>Current IP Address: ", "");
        String IPStr = IPStrNewest.replace("</body></html>", "");

但我只获得服务器的外部IP

but I get the external IP of the server only

和最后一个地方

        URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
        BufferedReader inIP = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
        String ip = inIP.readLine();

这是相同的,我只得到服务器的外部IP

this is the same, I get the external IP of the server only

那么,这笔交易是什么?

So, what's the deal?

推荐答案

如果您的客户使用 NAT (网络地址转换)它可能没有外部地址。大多数情况下,根据我的经验,情况就是这样。在工作中,我的Web请求通过代理,因此Web服务器只能确定此地址。在家里我通过服务器使用NAT,所以我输入的笔记本电脑没有外部地址。最接近的是从'whatismyip'返回的内容,我的服务器地址,通过它我有时会转发到我的笔记本电脑的端口。

If your client is using NAT (network address translation) it may not have an external address. Most often, in my experience, this is the case. At work, my web requests go through a proxy so the web server can only determine this address. At home I use NAT via a server so this laptop I'm typing on has no external address. The closest thing is what is returned from 'whatismyip', my server address, through which I may sometimes forward ports that go to my laptop.

这篇关于客户端的外部IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆