开发者

Get outgoing IP address in Java RMI

开发者 https://www.devze.com 2023-03-16 07:43 出处:网络
In my java RMI application, I can have instances that behave as servers both locally and on different hosts. In the latter case, how can I get my outgoing IP address so that I can set it as the argume

In my java RMI application, I can have instances that behave as servers both locally and on different hosts. 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?

Right now it's hard wired, but that's not a desirable solutio开发者_如何学Pythonn.


You can use the classic Java way:

try {
    InetAddress addr = InetAddress.getLocalHost();

    // Get IP Address
    byte[] ipAddr = addr.getAddress();

    // Get hostname
    String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}


The following will display all available non-loopback IPv4 adresses on a host. You can easily adapt it to get IPv6 addresses as well.

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);
}


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. The only time you need to set this property is if the default setting isn't correct: e.g. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消