getting ipv4 address from dual stack ipv6 and ipv4 host

Posted on 2007-10-18
Last Modified: 2013-12-14
Hi all

using Java 1.5.0_09

On windows vista (which has a dual stack IPv4 and IPv6 installed) using the code (below) only reveals the Ipv6 hostAddress. I need the IPv4 hostAddress.
Please assist

Enumeration NetEnum;
        StringBuilder buff = new StringBuilder();
        try {
            NetEnum = NetworkInterface.getNetworkInterfaces();
            Enumeration InetEnum = null;
            NetworkInterface MyNet = null;
            InetAddress MyInet  = null;
                MyNet = (NetworkInterface) NetEnum.nextElement();
                InetEnum =  MyNet.getInetAddresses();
                    MyInet = (InetAddress) InetEnum.nextElement();
                        mHostName = MyInet.getHostName();
                        mHostAddress =  MyInet.getHostAddress();
                        mInetAddress = MyInet;
                    buff.append(MyInet.getHostName() + " " + MyInet.getHostAddress()+"\n");
                    //System.out.println(MyInet.getHostName() + " " + MyInet.getHostAddress());
  "localhost: "+ mHostName + " " + mHostAddress);
        } catch (SocketException e) {
            // TODO Auto-generated catch block
Question by:stef4s
    LVL 9

    Expert Comment

    Have you tried using class?
    LVL 5

    Author Comment

    Yes I have. unfortunately when trying to convert addresses i get a Casting exception.

    I have however found a way to get the ipv4 address. (I don't understand why the code below returns a ipv4 address)

    So the solution is

                InetAddress ia = InetAddress.getLocalHost();
                mHostName = ia.getHostName();
                mHostAddress = ia.getHostAddress();
                mInetAddress = ia;
      "IP", mInetAddress);
            catch (UnknownHostException e)
                //System.out.println("oops, couldn't get localhost");
                log.error("could not get localhost");

    LVL 1

    Accepted Solution

    PAQed with points refunded (250)

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now