Get ip of localmachine

Discussion in 'Java' started by BigZero, Mar 6, 2008.

  1. BigZero

    BigZero Guest

    Hello,

    how do i get local machine ip,
    I tried this one the following code

    InetAddress group = InetAddress.getByName("localhost");
    System.out.println("\nip"+add.getHostAddress().toString());
    it returns loopback ip ie 127.0.0.1
    so plz any one can help me.....!









    Thanks
    Vijay
    BigZero, Mar 6, 2008
    #1
    1. Advertising

  2. On Wed, 5 Mar 2008 23:30:37 -0800 (PST), BigZero wrote:
    > how do i get local machine ip,


    Typically there are multiple IP addresses (often 2, sometimes more).
    Which one do you want?

    Iterate over the available java.net.NetworkInterfaces, ask each of
    them for its addresses, then choose one you like.

    /gordon

    --
    Gordon Beaton, Mar 6, 2008
    #2
    1. Advertising

  3. BigZero

    Peter Duniho Guest

    On Wed, 05 Mar 2008 23:30:37 -0800, BigZero <>
    wrote:

    > Hello,
    >
    > how do i get local machine ip,
    > I tried this one the following code
    >
    > InetAddress group = InetAddress.getByName("localhost");
    > System.out.println("\nip"+add.getHostAddress().toString());
    > it returns loopback ip ie 127.0.0.1
    > so plz any one can help me.....!


    Try InetAddress.getLocalHost()

    Yes, it's a little confusing. :)

    Pete
    Peter Duniho, Mar 6, 2008
    #3
  4. BigZero

    BigZero Guest

    Thanks

    i got it
    InetAddress group = InetAddress.getLocalHost();
    System.out.println ("Local IP : " + group.getHostAddress());

    Thanks

    Vijay
    BigZero, Mar 6, 2008
    #4
  5. BigZero

    Wayne Guest

    BigZero wrote:
    > Hello,
    >
    > how do i get local machine ip,
    > I tried this one the following code
    >
    > InetAddress group = InetAddress.getByName("localhost");
    > System.out.println("\nip"+add.getHostAddress().toString());
    > it returns loopback ip ie 127.0.0.1
    > so plz any one can help me.....!
    >
    > Thanks
    > Vijay


    <sscce>
    // "Quick and Dirty" Demo to show how to list a host's IP address(es).
    // Written 3/2008 by Wayne

    import java.net.*;
    import java.util.*;
    import static java.lang.System.out;

    public class ShowIPAddresses
    {
    public static void main ( String [] args ) throws Exception {
    InetAddress addr = InetAddress.getLocalHost();
    out.println( "My main IP is: " + addr.getHostAddress() + "\n" );

    out.println( "----------------------------" );

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

    while ( nics.hasMoreElements() ) {
    NetworkInterface nic = nics.nextElement();
    out.println( "IP addresses for NIC \"" + nic.getName() + "\" ("
    + nic.getDisplayName() + ")");
    for ( Enumeration<InetAddress> addrs = nic.getInetAddresses();
    addrs.hasMoreElements(); )
    out.println( "\t" + addrs.nextElement().getHostAddress() );
    }
    }
    }
    </sscce>
    Wayne, Mar 6, 2008
    #5
  6. BigZero

    Roedy Green Guest

    On Wed, 5 Mar 2008 23:30:37 -0800 (PST), BigZero
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >how do i get local machine ip,
    >I tried this one the following code


    see http://mindprod.com/jgloss/ip.html#OWNIP
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Mar 6, 2008
    #6
  7. BigZero

    BigZero Guest

    Thanks to one all










    Regards
    Vijay
    BigZero, Mar 6, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Curt_C [MVP]
    Replies:
    2
    Views:
    349
    Curt_C [MVP]
    Sep 21, 2004
  2. =?Utf-8?B?U2hhcmlm?=

    use anonymous but get get an authentication box

    =?Utf-8?B?U2hhcmlm?=, Sep 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    =?Utf-8?B?U2hhcmlm?=
    Sep 21, 2004
  3. Ryan Park
    Replies:
    2
    Views:
    671
    Joerg Jooss
    Jan 3, 2005
  4. Ryan Park
    Replies:
    0
    Views:
    380
    Ryan Park
    Jan 4, 2005
  5. Shiv
    Replies:
    3
    Views:
    811
    John C. Bollinger
    Oct 13, 2003
Loading...

Share This Page