Java and IP address on linux

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gurtd_dauberie@hta-bi.bfh.ch  to send email write

    Java and IP address on linux

    I can't get the correct IP address on Linux...
    The following code works properly on Windows and Unix.
    But on Linux, I always get the Loopback Address (127.0.0.1)
    Here is the code :

    //gets the IP via InetAddress class
    //works on UNIX and WIN, but not on LINUX !!

    InetAddress hostIP = InetAddress.get LocalHost();
    String hostIPStr = hostIP.getHostA ddress();

    Could anyone help ?
    Thanks in advance !
    dim
  • Tony Morris

    #2
    Re: Java and IP address on linux


    @see java.net.Networ kInterface#getN etworkInterface s()


    Comment

    • dual0

      #3
      Re: Java and IP address on linux

      gurtd@hta-bi.bfh.ch (gurtd_dauberie @hta-bi.bfh.ch to send email write without _dauberie) wrote in message news:<925e01f2. 0403290122.6746 ea7e@posting.go ogle.com>...[color=blue]
      > I can't get the correct IP address on Linux...
      > The following code works properly on Windows and Unix.
      > But on Linux, I always get the Loopback Address (127.0.0.1)
      > Here is the code :
      >
      > //gets the IP via InetAddress class
      > //works on UNIX and WIN, but not on LINUX !!
      >
      > InetAddress hostIP = InetAddress.get LocalHost();
      > String hostIPStr = hostIP.getHostA ddress();
      >
      > Could anyone help ?
      > Thanks in advance !
      > dim[/color]


      This is because you have a line in /etc/hosts like

      127.0.0.1 localhost

      you need to have

      xxx.xxx.xxx.xxx localhost

      and xxx.xxx.xxx.xxx is the ip address you binded to the iface you want.

      Comment

      • Jeff Schwab

        #4
        Re: Java and IP address on linux

        dual0 wrote:[color=blue]
        > gurtd@hta-bi.bfh.ch (gurtd_dauberie @hta-bi.bfh.ch to send email write without _dauberie) wrote in message news:<925e01f2. 0403290122.6746 ea7e@posting.go ogle.com>...
        >[color=green]
        >>I can't get the correct IP address on Linux...
        >>The following code works properly on Windows and Unix.
        >>But on Linux, I always get the Loopback Address (127.0.0.1)
        >>Here is the code :
        >>
        >> //gets the IP via InetAddress class
        >> //works on UNIX and WIN, but not on LINUX !!
        >>
        >> InetAddress hostIP = InetAddress.get LocalHost();
        >> String hostIPStr = hostIP.getHostA ddress();
        >>
        >>Could anyone help ?
        >>Thanks in advance !
        >>dim[/color]
        >
        >
        >
        > This is because you have a line in /etc/hosts like
        >
        > 127.0.0.1 localhost
        >
        > you need to have
        >
        > xxx.xxx.xxx.xxx localhost
        >
        > and xxx.xxx.xxx.xxx is the ip address you binded to the iface you want.[/color]

        Alternatively (e.g. if the system has DHCP), you could send a request to
        a site somewhere else on the internet that will send back the IP address
        originating the request. See myipaddress.com for an example of what I mean.

        Comment

        • Rob Jones

          #5
          Re: Java and IP address on linux

          dual0 wrote:[color=blue]
          > gurtd@hta-bi.bfh.ch (gurtd_dauberie @hta-bi.bfh.ch to send email write without _dauberie) wrote in message news:<925e01f2. 0403290122.6746 ea7e@posting.go ogle.com>...[color=green]
          >> I can't get the correct IP address on Linux...
          >> The following code works properly on Windows and Unix.
          >> But on Linux, I always get the Loopback Address (127.0.0.1)
          >> Here is the code :
          >>
          >> //gets the IP via InetAddress class
          >> //works on UNIX and WIN, but not on LINUX !!
          >>
          >> InetAddress hostIP = InetAddress.get LocalHost();
          >> String hostIPStr = hostIP.getHostA ddress();
          >>
          >> Could anyone help ?
          >> Thanks in advance !
          >> dim[/color]
          >
          >
          > This is because you have a line in /etc/hosts like
          >
          > 127.0.0.1 localhost
          >
          > you need to have
          >
          > xxx.xxx.xxx.xxx localhost
          >
          > and xxx.xxx.xxx.xxx is the ip address you binded to the iface you want.[/color]
          This would be very bad.
          You want to have
          127.0.0.1 localhost
          as that is the loopback address.
          IF your machine is 'arthur.somecom pany.com'
          with an IP address '192.168.120.1'
          then the /etc/hosts file should look like
          127.0.0.1 localhost
          192.168.120.1 arthur.somecomp any.com

          I seriously recommending not fixing a Java
          coding issues by mucking about with your Linux
          config files. Not only is it not right (and
          risks breaking some other network aware apps
          on your linux box) it will also not work anywhere
          else (unless the admins of those boxes are also
          like minded).

          Sorry I dont have a constructive Java solution
          for you but it is better that the solution be in
          Java.

          BTW the javadoc for
          InetAddress.get LocalHost();
          read "....an InetAddress representing the loopback address is returned."
          so it appears that the implementation of getLocalHost() is incorrect
          on your UNIX and WIN boxes. The loopback address is nearly allways 127.0.0.1

          Had a quick look for a solution for you.
          Found this comment
          "A host may have several IP addresses and other
          hosts may have to use different addresses to reach it. E.g. hosts on
          the intranet may have to use 192.168.0.100, but external machines may
          have to use 65.123.66.124.
          If you have a socket connection to another host you can call
          Socket.getLocal Address() to find out which local address is used for
          the socket."



          Comment

          • gurtd_dauberie@hta-bi.bfh.ch  to send email write

            #6
            Re: Java and IP address on linux

            Thanks guys for your answers !
            Have a nice day !
            gurtd

            Comment

            • Karl von Laudermann

              #7
              Re: Java and IP address on linux

              Rob Jones <nospam@dontbot her.com> wrote in message news:<c4gjq1$ci i$1@news.wplus. net>...
              [color=blue]
              > BTW the javadoc for
              > InetAddress.get LocalHost();
              > read "....an InetAddress representing the loopback address is returned."
              > so it appears that the implementation of getLocalHost() is incorrect
              > on your UNIX and WIN boxes. The loopback address is nearly allways 127.0.0.1[/color]

              Um, read the javadoc again. The part that you left out states that the
              loopback address is only returned if there is a security manager which
              disallows looking up the IP address.

              This may explain the behavior on Linux; perhaps Java on Linux by
              default uses a security manager that disallows getting the local IP
              address? I don't know much about security managers or why/when they
              are typically used.

              Comment

              • Chris

                #8
                Re: Java and IP address on linux

                -----BEGIN PGP SIGNED MESSAGE-----
                Hash: SHA1

                Karl von Laudermann wrote:

                [snip][color=blue]
                > Um, read the javadoc again. The part that you left out states that
                > the loopback address is only returned if there is a security manager
                > which disallows looking up the IP address.
                >
                > This may explain the behavior on Linux; perhaps Java on Linux by
                > default uses a security manager that disallows getting the local IP
                > address? I don't know much about security managers or why/when they
                > are typically used.[/color]

                Hi,
                I've been silently reading this thread, and the part I still don't get
                about getLocalHost(), is: which address?

                My computer has an Ethernet card and a dialup modem. At one time, my
                Ethernet card had address 10.32.156.1 (static), address 192.168.0.1
                (static), and a DHCP address from my ISP via ADSL. I could also dial
                my modem to another ISP and get a fourth (dynamic) IP address.

                Which one is mine? Which one should getLocalHost() return? Every
                single one of those IP addresses was equally legitimately MY address.

                That's (I have to assume) why getNetworkInter faces() was invented. I
                suggest using that.

                - --
                Chris
                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.2.2 (GNU/Linux)

                iD8DBQFAbdUOnwj A8LryK2IRAs4IAK CHg5T5g3Nw2uhVE Dxh9oc5jqjRggCf T4mI
                TIZ/IbatULhUit/6mZgH7Yg=
                =n7Ws
                -----END PGP SIGNATURE-----

                Comment

                Working...