how can I know the IP of the local computer

Discussion in 'Java' started by lrantisi, Nov 12, 2006.

  1. lrantisi

    lrantisi Guest

    Dear friends
    how can I know the IP of the local computer, using java InetAddress
    maybe or another object.
    lrantisi, Nov 12, 2006
    #1
    1. Advertising

  2. lrantisi wrote:
    > how can I know the IP of the local computer, using java InetAddress
    > maybe or another object.


    Simple:

    import java.net.*;

    public class Localhost {
    public static void main(String[] args) throws Exception {
    System.out.println(InetAddress.getLocalHost().getHostAddress());
    }
    }

    more advanced:

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

    public class NI15 {
    public static void main(String[] args) throws Exception {
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while(e.hasMoreElements()) {
    NetworkInterface ni = (NetworkInterface)e.nextElement();
    System.out.println("Net interface: " + ni.getName());
    Enumeration e2 = ni.getInetAddresses();
    while (e2.hasMoreElements()){
    InetAddress ip = (InetAddress)e2.nextElement();
    System.out.println("IP address: " + ip.getHostAddress());
    }
    }
    }
    }

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 12, 2006
    #2
    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. =?Utf-8?B?c21lbg==?=

    can i know a computer name?

    =?Utf-8?B?c21lbg==?=, Mar 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    351
    Martha[MSFT]
    Mar 6, 2004
  2. anon
    Replies:
    3
    Views:
    4,322
  3. salon
    Replies:
    4
    Views:
    389
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    May 30, 2007
  4. Lionel Neo

    Display html on another computer instead of local computer

    Lionel Neo, Oct 11, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    228
    Robert Koritnik
    Oct 11, 2004
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    227
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page