Java获取我的IP地址
我试图用Java获取我的Internet IP地址,但当我的IP地址为192.168.0.xxx时,我一直获取我的本地地址(即:127.0.0.1) 我在用电话:Java获取我的IP地址,java,ip,wifi,Java,Ip,Wifi,我试图用Java获取我的Internet IP地址,但当我的IP地址为192.168.0.xxx时,我一直获取我的本地地址(即:127.0.0.1) 我在用电话: InetAddress.getLocalHost().getHostAddress(); 这似乎是标准的IP地址,但它不是我要找的。每个教程都说要使用这一行,所以我有点困惑 谁能告诉我怎样才能得到正确的IP地址吗 我在连接到WiFi的设备上运行,并且我没有使用任何电缆。我正在使用ifconfig inet addr提供的IP连接到
InetAddress.getLocalHost().getHostAddress();
这似乎是标准的IP地址,但它不是我要找的。每个教程都说要使用这一行,所以我有点困惑
谁能告诉我怎样才能得到正确的IP地址吗
我在连接到WiFi的设备上运行,并且我没有使用任何电缆。我正在使用ifconfig inet addr提供的IP连接到服务器,并希望获得设备的inet addr。我可以在服务器端检查套接字的IP,但我认为如果设备(客户端)告诉服务器他希望其他设备连接的IP会更好 该类包含所有相关方法,但请注意,没有“我的IP”这样的东西。一台机器可以有多个接口,每个接口可以有多个IP 您可以使用这个类列出所有这些接口和IP,但是从列表中选择哪个接口和IP取决于您到底需要使用这个IP做什么
(
InetAddress.getLocalHost()
不咨询您的接口,它只返回常量127.0.0.1(对于IPv4))另一个默认网络接口选项,就在我5分钟前尝试时看到了您的问题:)
InetAddress[]localaddr;
试一试{
localaddr=InetAddress.getAllByName(“host.name”);
for(int i=0;i
字符串ip;
试一试{
枚举接口=NetworkInterface.getNetworkInterfaces();
while(interfaces.hasMoreElements()){
NetworkInterface iface=interfaces.nextElement();
//过滤掉127.0.0.1和非活动接口
if(iface.isLoopback()| |!iface.isUp())
继续;
枚举地址=iface.getInetAddresses();
while(addresses.hasMoreElements()){
InetAddress addr=地址.nextElement();
ip=addr.getHostAddress();
System.out.println(iface.getDisplayName()+“”+ip);
}
}
}捕获(SocketException e){
抛出新的运行时异常(e);
}
遇到同样的问题,在本页找到了解决方案:
从长远来看,这段代码有一些问题,一周内有好几次服务器都不会回复
新解决方案:
public static String getIpAddress()
{
URL myIP;
try {
myIP = new URL("http://api.externalip.net/ip/");
BufferedReader in = new BufferedReader(
new InputStreamReader(myIP.openStream())
);
return in.readLine();
} catch (Exception e)
{
try
{
myIP = new URL("http://myip.dnsomatic.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(myIP.openStream())
);
return in.readLine();
} catch (Exception e1)
{
try {
myIP = new URL("http://icanhazip.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(myIP.openStream())
);
return in.readLine();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return null;
}
让我们问问AWS
编辑
在你否决之前,我很清楚这不是一个java解决方案。它是任何编程语言的通用解决方案。其他解决方案对我来说不太管用。另外,我相信了解你的IP更容易的方法是上网。它可以是任何站点,服务器可以返回它在请求中获得的客户端ip。您可以为它设置自己的端点。//此程序是查找您的确切LAN(运行此程序的本地计算机)IP地址
//This program is find your exact LAN(Local Machine on which your are //runing this program) IP Address
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class GetMyIPAddress {
public static void main(String gks[]) throws SocketException{
Enumeration e = NetworkInterface.getNetworkInterfaces();
int ctr=0;
while(e.hasMoreElements())
{
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements() && ctr<3)
{ ctr++;
if(ctr==3)
break;
InetAddress i = (InetAddress) ee.nextElement();
if(ctr==2)
System.out.println(i.getHostAddress());
}
}
}
}
导入java.net.InetAddress;
导入java.net.NetworkInterface;
导入java.net.SocketException;
导入java.util.Enumeration;
公共类GetMyIPAddress{
publicstaticvoidmain(字符串gks[])抛出SocketException{
枚举e=NetworkInterface.getNetworkInterfaces();
int ctr=0;
而(e.hasMoreElements())
{
网络接口n=(网络接口)e.nextElement();
枚举ee=n.getInetAddresses();
而(ee.hasMoreElements()&&ctr我的解决方案只返回1个Ip4地址:
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
if (iface.isLoopback() || !iface.isUp() || iface.isVirtual() || iface.isPointToPoint())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
final String ip = addr.getHostAddress();
if(Inet4Address.class == addr.getClass()) return ip;
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
return null;
试试看{
枚举接口=NetworkInterface.getNetworkInterfaces();
while(interfaces.hasMoreElements()){
NetworkInterface iface=interfaces.nextElement();
if(iface.isLoopback()| | |!iface.isUp()| | iface.isVirtual()| | iface.isPointToPoint())
继续;
枚举地址=iface.getInetAddresses();
while(addresses.hasMoreElements()){
InetAddress addr=地址.nextElement();
最后一个字符串ip=addr.getHostAddress();
if(Inet4Address.class==addr.getClass())返回ip;
}
}
}捕获(SocketException e){
抛出新的运行时异常(e);
}
返回null;
以下是我获取IP地址的方法
获取您的默认网关地址
获取电脑中的所有地址
现在,在您的IP(假设192.168.100.4)和默认网关IP(假设192.168.100.1)中,地址的前9位数字必须相同,因此满足此条件的任何IP都是您的IP
请参阅下面的完整工作代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class MyIpAddress {
public static void main(String[] args) {
// doPortForwarding();
MyIpAddress myIpAddress = new MyIpAddress();
// get default address
String yourIp = myIpAddress.getYourIp(myIpAddress
.getDefaultGateWayAddress());
System.out.println(yourIp);
// get
} // amin
// return ip address for which u need to do port forwarding
private String getYourIp(String defaultAddress) {
String temp = defaultAddress.substring(0, 11);
String ipToForward = "";
TreeSet<String> ipAddrs = getIpAddressList();
for (Iterator<String> iterator = ipAddrs.iterator(); iterator.hasNext();) {
String tempIp = iterator.next();
if (tempIp.contains(temp)) {
ipToForward = tempIp;
break;
}
}
return ipToForward;
}// ipForPortForwarding
// get the ipaddress list
private TreeSet<String> getIpAddressList() {
TreeSet<String> ipAddrs = new TreeSet<String>();
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface
.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ipAddrs.add(addr.getHostAddress());
}// 2 nd while
}// 1 st while
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ipAddrs;
}// getIpAddressList
// get default gateway address in java
private String getDefaultGateWayAddress() {
String defaultAddress = "";
try {
Process result = Runtime.getRuntime().exec("netstat -rn");
BufferedReader output = new BufferedReader(new InputStreamReader(
result.getInputStream()));
String line = output.readLine();
while (line != null) {
if (line.contains("0.0.0.0")) {
StringTokenizer stringTokenizer = new StringTokenizer(line);
stringTokenizer.nextElement();// first string is 0.0.0.0
stringTokenizer.nextElement();// second string is 0.0.0.0
defaultAddress = (String) stringTokenizer.nextElement(); // this is our default address
break;
}
line = output.readLine();
}// while
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return defaultAddress;
}// getDefaultAddress
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.net.InetAddress;
导入java.net.NetworkInterface;
导入java.net.SocketException;
导入java.util.Enumeration;
导入java.util.Iterator;
导入java.util.StringTokenizer;
导入java.util.TreeSet;
公共类MyIpAddress{
公共静态void main(字符串[]args){
//doPortForwarding();
MyIpAddress MyIpAddress=新的MyIpAddress();
//获取默认地址
字符串yourIp=myIpAddress.getYourIp(myIpAddress
.getDefaultGateWayAddress());
System.out.println(yourIp);
//得到
}//阿明
//返回您需要进行端口转发的ip地址
私有字符串getYourIp(字符串默认地址){
String temp=defaultAddress.substring(0,11);
字符串ipToForward=“”;
TreeSet ipAddrs=getIpAddressList();
for(Iterator Iterator=ipAddrs.Iterator();Iterator.hasNext();){
字符串tempIp=iterator.next();
如果(临时包含(临时)){
ipToForward=tempIp;
打破
}
}
返回方向;
}//ipForPortForwarding
//获取IP地址列表
URL url = new URL("http://checkip.amazonaws.com/");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println(br.readLine());
//This program is find your exact LAN(Local Machine on which your are //runing this program) IP Address
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class GetMyIPAddress {
public static void main(String gks[]) throws SocketException{
Enumeration e = NetworkInterface.getNetworkInterfaces();
int ctr=0;
while(e.hasMoreElements())
{
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements() && ctr<3)
{ ctr++;
if(ctr==3)
break;
InetAddress i = (InetAddress) ee.nextElement();
if(ctr==2)
System.out.println(i.getHostAddress());
}
}
}
}
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
if (iface.isLoopback() || !iface.isUp() || iface.isVirtual() || iface.isPointToPoint())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
final String ip = addr.getHostAddress();
if(Inet4Address.class == addr.getClass()) return ip;
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
return null;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class MyIpAddress {
public static void main(String[] args) {
// doPortForwarding();
MyIpAddress myIpAddress = new MyIpAddress();
// get default address
String yourIp = myIpAddress.getYourIp(myIpAddress
.getDefaultGateWayAddress());
System.out.println(yourIp);
// get
} // amin
// return ip address for which u need to do port forwarding
private String getYourIp(String defaultAddress) {
String temp = defaultAddress.substring(0, 11);
String ipToForward = "";
TreeSet<String> ipAddrs = getIpAddressList();
for (Iterator<String> iterator = ipAddrs.iterator(); iterator.hasNext();) {
String tempIp = iterator.next();
if (tempIp.contains(temp)) {
ipToForward = tempIp;
break;
}
}
return ipToForward;
}// ipForPortForwarding
// get the ipaddress list
private TreeSet<String> getIpAddressList() {
TreeSet<String> ipAddrs = new TreeSet<String>();
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface
.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
// filters out 127.0.0.1 and inactive interfaces
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ipAddrs.add(addr.getHostAddress());
}// 2 nd while
}// 1 st while
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ipAddrs;
}// getIpAddressList
// get default gateway address in java
private String getDefaultGateWayAddress() {
String defaultAddress = "";
try {
Process result = Runtime.getRuntime().exec("netstat -rn");
BufferedReader output = new BufferedReader(new InputStreamReader(
result.getInputStream()));
String line = output.readLine();
while (line != null) {
if (line.contains("0.0.0.0")) {
StringTokenizer stringTokenizer = new StringTokenizer(line);
stringTokenizer.nextElement();// first string is 0.0.0.0
stringTokenizer.nextElement();// second string is 0.0.0.0
defaultAddress = (String) stringTokenizer.nextElement(); // this is our default address
break;
}
line = output.readLine();
}// while
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return defaultAddress;
}// getDefaultAddress
}
Document doc = Jsoup.connect("https://whatismyipaddress.com/").timeout(10000).get() ;
Elements el = doc.select("div#section_left") ;
Element e = el.select("a").get(
System.out.println(e.text());
import java.net.InetAddress;
public class Main {
public static void main(String[] args) throws Exception
{
System.out.println(InetAddress.getLocalHost());
}
}