Getting the IP address of the current machine using Java

后端 未结 17 1779
眼角桃花
眼角桃花 2020-11-22 02:26

I am trying to develop a system where there are different nodes that are run on different system or on different ports on the same system.

Now all the nodes create

17条回答
  •  逝去的感伤
    2020-11-22 03:21

    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.util.Enumeration;
    
    public class IpAddress {
    
    NetworkInterface ifcfg;
    Enumeration addresses;
    String address;
    
    public String getIpAddress(String host) {
        try {
            ifcfg = NetworkInterface.getByName(host);
            addresses = ifcfg.getInetAddresses();
            while (addresses.hasMoreElements()) {
                address = addresses.nextElement().toString();
                address = address.replace("/", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ifcfg.toString();
    }
    }
    

提交回复
热议问题