Java InetAddress Create getLocalInetAddress()

Here you can find the source of getLocalInetAddress()

Description

get Local Inet Address

License

Open Source License

Declaration

public static InetAddress getLocalInetAddress() 

Method Source Code


//package com.java2s;
/*Errare is a free and crossplatform MMORPG project.
Copyright (C) 2006  Antoine PIERRONNET/*from  www.j a  v  a 2s .  c o  m*/
    
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.*/

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class Main {
    public static InetAddress getLocalInetAddress() {
        NetworkInterface iface = null;
        if (System.getProperty("os.name").compareTo("Linux") != 0)
            try {
                return InetAddress.getLocalHost();
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            }
        try {
            for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
                iface = (NetworkInterface) ifaces.nextElement();
                System.out.println("Interface:" + iface.getDisplayName());
                InetAddress ia = null;
                InetAddress temp = null;
                for (Enumeration ips = iface.getInetAddresses(); ips.hasMoreElements();) {
                    temp = (InetAddress) ips.nextElement();
                    if (!temp.isLoopbackAddress())
                        ia = temp;
                }
                return ia;
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. getIP(InetAddress ip)
  2. getIPAsLong(InetAddress address)
  3. getIPAsString(InetAddress addr)
  4. getIPv4LocalNetMask(InetAddress ip, int netPrefix)
  5. getIPv6InetAddresses(NetworkInterface networkInterface)
  6. getLocalInetAddress()
  7. getLocalInetAddress()
  8. getLocalInetAddress(String host)
  9. getLocalInetAddress(String host)