我正在尝试开发一个系统,其中有不同的节点在不同的系统或同一系统上的不同端口上运行.
现在,所有节点都创建一个Socket,其目标IP作为称为引导节点的特殊节点的IP.然后节点创建自己的节点ServerSocket
并开始侦听连接.
引导节点维护一个节点列表,并在被查询时返回它们.
现在我需要的是节点必须将其IP注册到自举节点.我尝试使用cli.getInetAddress()
一旦客户端连接到ServerSocket
bootstrapping节点,但这不起作用.
使用代码:
System.out.println(Inet4Address.getLocalHost().getHostAddress());
Run Code Online (Sandbox Code Playgroud)
要么
System.out.println(InetAddress.getLocalHost().getHostAddress());
Run Code Online (Sandbox Code Playgroud)
我的PPP连接IP地址是:117.204.44.192,但上面的返回值为192.168.1.2
编辑
我使用以下代码:
Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements())
{
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements())
{
InetAddress i = (InetAddress) ee.nextElement();
System.out.println(i.getHostAddress());
}
}
Run Code Online (Sandbox Code Playgroud)
我能够获得所有相关的所有IP地址NetworkInterface
,但我如何区分它们?这是我得到的输出:
127.0.0.1
192.168.1.2
192.168.56.1
117.204.44.19
Run Code Online (Sandbox Code Playgroud) 我不太确定如何获取机器的外部IP地址,因为网络外的计算机会看到它.
我的以下IPAddress类仅获取计算机的本地IP地址.
public class IPAddress {
private InetAddress thisIp;
private String thisIpAddress;
private void setIpAdd() {
try {
InetAddress thisIp = InetAddress.getLocalHost();
thisIpAddress = thisIp.getHostAddress().toString();
} catch (Exception e) {
}
}
protected String getIpAddress() {
setIpAdd();
return thisIpAddress;
}
}
Run Code Online (Sandbox Code Playgroud) 我遇到一个简单问题的麻烦.我会在HTTPServlet中获得真正的客户端IP.
从现在起我用过:
request.getRemoteAddr()
Run Code Online (Sandbox Code Playgroud)
但现在它返回一个虚假的IP.例如:xxx.xxx.xxx.50,但我的IP类似于xxx.xxx.xxx.159.(查看http://whatismyipaddress.com/).
现在我试着用:
request.getHeader("X-Forwarded-For")
Run Code Online (Sandbox Code Playgroud)
它返回NULL.
我还参加了以下课程的探测:
public class IpUtils {
public static final String _255 = "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
public static final Pattern pattern = Pattern.compile("^(?:" + _255 + "\\.){3}" + _255 + "$");
public static String longToIpV4(long longIp) {
int octet3 = (int) ((longIp >> 24) % 256);
int octet2 = (int) ((longIp >> 16) % 256);
int octet1 = (int) ((longIp >> 8) % 256);
int octet0 = …
Run Code Online (Sandbox Code Playgroud) 阅读后:用Java获取"外部"IP地址
码:
public static void main(String[] args) throws IOException
{
URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
String ip = in.readLine(); //you get the IP as a String
System.out.println(ip);
}
Run Code Online (Sandbox Code Playgroud)
我以为我是胜利者,但我得到以下错误
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://automation.whatismyip.com/n09230945.asp
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at getIP.main(getIP.java:12)
Run Code Online (Sandbox Code Playgroud)
我认为这是因为服务器没有足够快的响应,无论如何确保它将获得外部IP?
编辑:好吧所以它被拒绝,其他人都知道另一个可以做同样功能的网站