[英]Get outgoing IP address in Java RMI
In my java RMI application, I can have instances that behave as servers both locally and on different hosts.在我的 java RMI 应用程序中,我可以拥有在本地和不同主机上充当服务器的实例。 In the latter case, how can I get my outgoing IP address so that I can set it as the argument of system property java.rmi.server.hostName?
在后一种情况下,如何获取传出的 IP 地址,以便将其设置为系统属性 java.rmi.server.hostName 的参数?
Right now it's hard wired, but that's not a desirable solution.现在它是硬连线的,但这不是一个理想的解决方案。
You can use the classic Java way:您可以使用经典的 Java 方式:
try {
InetAddress addr = InetAddress.getLocalHost();
// Get IP Address
byte[] ipAddr = addr.getAddress();
// Get hostname
String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}
I don't understand why you think you need this.我不明白你为什么认为你需要这个。 The source-address in outgoing stubs is set to the host's IP address automatically.
传出存根中的源地址自动设置为主机的 IP 地址。 The only time you need to set this property is if the default setting isn't correct: eg if you have multiple NICs and only want to export via one, or if you have the Linux misconfiguration that maps your hostname to 127.0.0.1 instead of your IP address.
唯一需要设置此属性的情况是默认设置不正确:例如,如果您有多个 NIC 并且只想通过一个导出,或者如果您有 Linux 错误配置,将您的主机名映射到 127.0.0.1 而不是您的 IP 地址。
The following will display all available non-loopback IPv4 adresses on a host.下面将显示主机上所有可用的非环回 IPv4 地址。 You can easily adapt it to get IPv6 addresses as well.
您也可以轻松地对其进行调整以获取 IPv6 地址。
public static void main(String...args) {
List<InetAddress> addresses = getNonLocalIPV4Addresses();
for (InetAddress addr: addresses) {
System.out.println("address: " + addr.getHostAddress());
}
}
/**
* Get a list of all known non-local IP v4 addresses for the current host.
* @return a List of <code>InetAddress</code>, may be empty but never null.
*/
public static List<InetAddress> getNonLocalIPV4Addresses() {
return getIPAddresses(new InetAddressFilter() {
public boolean accepts(InetAddress addr) {
return (addr instanceof Inet4Address)
&& !(addr.isLoopbackAddress()) || "localhost".equals(addr.getHostName()));
}
});
}
/**
* Get a list of all known IP addresses for the current host,
* according to the specified filter.
* @param filter filters out unwanted addresses.
* @return a List of <code>InetAddress</code>, may be empty but never null.
*/
public static List<InetAddress> getIPAddresses(InetAddressFilter filter) {
List<InetAddress> list = new ArrayList<InetAddress>();
try {
Enumeration<NetworkInterface> interfaces =
NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
if ((filter == null) || filter.accepts(addr)) list.add(addr);
}
}
} catch(Exception e) {
e.printStackTrace();
}
return list;
}
/**
* Filter interface for the methods discovering available IP addresses.
*/
public interface InetAddressFilter {
/**
* Determine whether the specified address is accepted.
* @param addr the address to check.
* @return true if the address is accepted, false otherwise.
*/
boolean accepts(InetAddress addr);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.