How to get my IP address

Discussion in 'Java' started by Neutrino, Nov 2, 2004.

  1. Neutrino

    Neutrino Guest

    Hello,
    I try to read my IP address, under Windows XP, the function
    InetAddress.getLocalHost().getHostAddress() return the correct address but
    with Linux Red Hat the same function return 127.0.01
    How to get the correct address under Linux ?
    Thanks
    AR
     
    Neutrino, Nov 2, 2004
    #1
    1. Advertising

  2. Neutrino wrote:
    > I try to read my IP address, under Windows XP, the function
    > InetAddress.getLocalHost().getHostAddress() return the correct address but
    > with Linux Red Hat the same function return 127.0.01
    > How to get the correct address under Linux ?


    Which is the "correct address"? 127.0.0.1 sounds perfectly correct to me.
    One computer can have *a lot* of IP addesses.

    You can get all of them via NetworkInterface.getNetworkInterfaces()
    and then getInetAddresses() on all of the results.
     
    Michael Borgwardt, Nov 2, 2004
    #2
    1. Advertising

  3. Neutrino

    Neutrino Guest

    Thank you for your answer,
    I agree with you, 127.0.01 is correct, but inside my program I have to
    connect to a host specifying the local port, so I decided to use the
    function : socket (host, remote_port, local, local_port). In a order to get
    the local address, I used local = InetAddress.getLocalHost(); this is
    working perfectly with Windows XP (java 1.4.2), but with Linux Red Hat, the
    connection fails with a Socket Exception "Invalid argument or Cannot assign
    requested address". If I change the IP address 127.0.0.1 in the local
    InetAddress with the IP address of my interface (I have only one net
    interface), the connection is ok. I don't understand why ?
    Best regards,
    AR

    > Which is the "correct address"? 127.0.0.1 sounds perfectly correct to me.
    > One computer can have *a lot* of IP addesses.
     
    Neutrino, Nov 3, 2004
    #3
  4. On Wed, 3 Nov 2004 09:57:16 +0100, Neutrino wrote:
    > I agree with you, 127.0.01 is correct, but inside my program I have to
    > connect to a host specifying the local port, so I decided to use the
    > function : socket (host, remote_port, local, local_port).


    The easiest and most general solution to your problem is to specify
    only the local port, not the local address. Here is one way:

    Socket s = new Socket(); // unconnected socket!
    s.bind(new InetSocketAddress(localPort));
    s.connect(new InetSocketAddress(remoteHost, remotePort));

    > If I change the IP address 127.0.0.1 in the local InetAddress with
    > the IP address of my interface (I have only one net interface), the
    > connection is ok. I don't understand why ?


    No, you have at least 2 interfaces. lo (with address 127.0.0.1) and
    another one, with a different address. You can type "ifconfig -a" to
    see all of your interfaces and their addresses.

    The local address you choose must be one from which the remote address
    is reachable, but 127.0.0.1 isn't such an address (have a look at your
    routing table: "netstat -r").

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Nov 3, 2004
    #4
  5. Neutrino

    Tim Ward Guest

    "Neutrino" <> wrote in message
    news:41889d85$0$3530$...
    > Thank you for your answer,
    > I agree with you, 127.0.01 is correct, but inside my program I have to
    > connect to a host specifying the local port, so I decided to use the
    > function : socket (host, remote_port, local, local_port). In a order to

    get
    > the local address, I used local = InetAddress.getLocalHost(); this is
    > working perfectly with Windows XP (java 1.4.2), but with Linux Red Hat,

    the
    > connection fails with a Socket Exception "Invalid argument or Cannot

    assign
    > requested address". If I change the IP address 127.0.0.1 in the local
    > InetAddress with the IP address of my interface (I have only one net
    > interface), the connection is ok. I don't understand why ?


    The world is like that, that's why. Typically your machine will have many IP
    addresses, of which 127.0.0.1 is quite often one. Any API that tries to
    return "the" IP address of the machine just guesses which one you want, and
    different APIs (or even the same API on different operating systems) make
    different guesses.

    (Even when you managed to work out which is the right one of your IP
    addresses you're not finished if you also need the subnet mask - last time I
    tried it there appeared to be no (portable) way to get this in Java.)

    --
    Tim Ward
    Brett Ward Limited - www.brettward.co.uk
     
    Tim Ward, Nov 3, 2004
    #5
    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. avnrao
    Replies:
    1
    Views:
    7,246
    avnrao
    May 4, 2004
  2. Patrice
    Replies:
    0
    Views:
    895
    Patrice
    May 4, 2004
  3. Raterus
    Replies:
    0
    Views:
    443
    Raterus
    Jun 2, 2004
  4. Alex Stevens
    Replies:
    0
    Views:
    784
    Alex Stevens
    Aug 10, 2004
  5. =?Utf-8?B?QW5nZWw=?=

    Pass a Web Address and return a File System Address

    =?Utf-8?B?QW5nZWw=?=, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    545
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=
    Sep 21, 2005
Loading...

Share This Page