How do I get IP address of localhost?

Category: java.net, viewed: 8288 time(s).
import java.net.InetAddress;
import java.net.UnknownHostException;

public class IPAddressExample
{
    public static void main(String[] args)
    {
        try
        {
            InetAddress address = InetAddress.getLocalHost();

            byte[] ip = address.getAddress();

            int i = 4;
            String ipAddress = "";			
            for (byte b : ip)
            {
                ipAddress += (b & 0xFF);
                if (--i > 0)
                {
                    ipAddress += ".";
                }
            }

            System.out.println(ipAddress);
        } catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats