Example usage for java.net NetworkInterface getInetAddresses

List of usage examples for java.net NetworkInterface getInetAddresses

Introduction

In this page you can find the example usage for java.net NetworkInterface getInetAddresses.

Prototype

public Enumeration<InetAddress> getInetAddresses() 

Source Link

Document

Get an Enumeration with all or a subset of the InetAddresses bound to this network interface.

Usage

From source file:Main.java

static public void main(String args[]) throws Exception {
    int port = 80;

    NetworkInterface ni = NetworkInterface.getByName("name");
    Enumeration e = ni.getInetAddresses();
    if (!e.hasMoreElements())
        return;/*from w  ww.j ava  2  s  .c  o  m*/
    InetAddress ia = (InetAddress) e.nextElement();

    ServerSocket ss = new ServerSocket(port, 20, ia);
    System.out.println("Listening");
    Socket s = ss.accept();
    System.out.println(s);
}

From source file:com.netsteadfast.greenstep.util.HostUtils.java

public static void main(String args[]) throws Exception {
    Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
    for (; nics.hasMoreElements();) {
        System.out.println("--------------------------------------------------------------------------");
        NetworkInterface interfece = nics.nextElement();
        System.out.println(interfece.getName());
        Enumeration<InetAddress> addrs = interfece.getInetAddresses();
        for (; addrs.hasMoreElements();) {
            InetAddress addr = addrs.nextElement();
            System.out.println(addr.getHostAddress());
        }/*from  ww w.  j  a  va2 s  . c  o m*/
    }
}

From source file:uk.ac.horizon.ubihelper.j2se.Server.java

/**
 * @param args//  w w  w  . ja  v a2s.  c o  m
 */
public static void main(String[] args) {
    InetAddress bestAddress = null;
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            if (!ni.isUp() || ni.isVirtual() || ni.isLoopback())
                continue;
            logger.info("Has interface " + ni.getName() + ": " + ni.getDisplayName());
            Enumeration<InetAddress> as = ni.getInetAddresses();
            while (as.hasMoreElements()) {
                InetAddress a = as.nextElement();
                if (a instanceof Inet4Address) {
                    Inet4Address ip = (Inet4Address) a;
                    logger.info("- IPv4 address " + ip.getHostAddress());
                    if (ip.isSiteLocalAddress()) {
                        logger.info("-- site local!");
                        if (bestAddress == null)
                            bestAddress = ip;
                    } else
                        bestAddress = ip;
                }
            }
        }
    } catch (Exception e) {
        logger.severe("Could not list NetworkInterfaces: " + e);
        System.exit(-1);
    }
    if (bestAddress == null) {
        logger.severe("Could not find an IP address to bind to - using localhost");
        try {
            bestAddress = InetAddress.getLocalHost();
        } catch (Exception e) {
            logger.severe("Could not get localhost address: " + e);
            System.exit(-2);
        }
    }
    int port = 0;
    if (args.length == 1) {
        try {
            port = Integer.parseInt(args[0]);
        } catch (NumberFormatException nfe) {
            System.err.println("Usage: <server-port>");
            System.exit(-3);
        }
    }
    Server server = new Server();
    server.init(bestAddress, port);
}

From source file:Main.java

public static String getHostIp() {
    try {/*from ww w  .ja va 2s. c  o  m*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr.hasMoreElements();) {
                InetAddress inetAddress = ipAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() throws SocketException {
    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
        NetworkInterface intf = en.nextElement();
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
            InetAddress inetAddress = enumIpAddr.nextElement();
            if (!inetAddress.isLoopbackAddress()) {
                return inetAddress.getHostAddress().toString();
            }//from   w  w  w  .  ja v a2  s  . c  o m
        }
    }
    return null;
}

From source file:Main.java

@Nullable
public static String getLocalIPAddress() {
    try {//from ww w.  java2s .  co m
        for (Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces(); eni
                .hasMoreElements();) {
            NetworkInterface ni = eni.nextElement();
            for (Enumeration<InetAddress> eia = ni.getInetAddresses(); eia.hasMoreElements();) {
                InetAddress ia = eia.nextElement();
                if (!ia.isLoopbackAddress() && (ia instanceof Inet4Address)) {
                    return ia.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() throws Exception {
    String ipAddress = null;/*  www . j  av a2  s  .  c  o  m*/

    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();

    while (en.hasMoreElements()) {
        NetworkInterface e = en.nextElement();
        Enumeration<InetAddress> addresses = e.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) {
                ipAddress = address.getHostName();
                break;
            }
        }
    }

    if (ipAddress == null) {
        ipAddress = InetAddress.getLocalHost().getHostAddress();
    }

    return ipAddress;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {//from  w w  w .ja  va 2s . co m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        System.out.println("WifiPreference IpAddress" + ex.toString());
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {//from w w  w. j  a v a2 s .  c  om
        Enumeration<NetworkInterface> networkInfo = NetworkInterface.getNetworkInterfaces();
        for (Enumeration<NetworkInterface> en = networkInfo; en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            Enumeration<InetAddress> intfAddress = intf.getInetAddresses();
            for (Enumeration<InetAddress> enumIpAddr = intfAddress; enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {

    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {/*from www .java 2s .c  o  m*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return "";
}