9 Replies Latest reply: Mar 5, 2011 9:26 PM by EJP RSS

    Obtain IP Address

    831550
      Hello all,

      In my application I want to display the ip address of the current machine in the textbox.
      I have used the following code for that.
      txtIP = new JTextField(InetAddress.getLocalHost().toString());
      The problem is that it returns the ip address as 192.168.xxx.xxx whereas my ip address is 116.xxx.xxx.xxx.
      What is the reason for this?
      How do I obtain the latter ip address?

      My application works well if it is implemented on LAN and also on internet but I am unable to obtain the latter ip address which is needed if the application has to work on internet!

      Reply if I am not clear in my question.

      Thanking you all.
        • 1. Re: Obtain IP Address
          EJP
          The IP address of your computer is 192.xxx.xxx.xxx.

          The IP address of your router is 116.xxx.xxx.xxx. That information isn't available to Java.
          • 2. Re: Obtain IP Address
            831550
            So there's no way to obtain that then?
            • 3. Re: Obtain IP Address
              YoungWinston
              aLkeshP wrote:
              The problem is that it returns the ip address as 192.168.xxx.xxx whereas my ip address is 116.xxx.xxx.xxx.
              What is the reason for this?
              Because, unless your machine is connected directly to the Internet (unusual), the 116 address will be the address of your Internet switch/modem.
              How do I obtain the latter ip address?
              Not sure. It might be visible in your routing table, but you should probably look at the manual for your box.

              Winston

              Edited by: YoungWinston on Mar 5, 2011 2:56 PM

              Also, why do you need to know this? Seems a bit iffy to me.
              • 4. Re: Obtain IP Address
                baftos
                This is your external ip address:
                http://www.whatismyip.com/automation/n09230945.asp
                • 5. Re: Obtain IP Address
                  831550
                  Thanks for the reply..

                  I'm developing an application which is based on server-client relation model wherein the client needs to put that ip address in the textfield and the port number on which the server is listening. This informatiion is visible to the person sitting on the server in a readonly text field which he will provide to the client. The client then can establish the connection with the server.
                  Now this could be the situation in which any computer could be server and any computer could be the client in the internet. So neither the server is predecided nor the client.

                  Hence I need to obtain the server's ip address to establish the connection.

                  I hope I am clear enough.
                  Thanking you.
                  • 6. Re: Obtain IP Address
                    baftos
                    I thought my hint was enough, but I realize we are in New to Java, so let me be more precise.
                    I don't understand why you need this, but I can imagine some legitimate cases.
                    This web site is there at least since ~2006 when I found out about it.
                    It's still there, so there should be no problem, but you never know...
                    I am hinting at some code like this:
                    URL url = new URL("http://www.whatismyip.com/automation/n09230945.asp");
                    InputStream is = url.openConnection().getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String ipStr = br.readLine();
                    • 7. Re: Obtain IP Address
                      jschellSomeoneStoleMyAlias
                      >
                      I'm developing an application which is based on server-client relation model wherein the client needs to put that ip address in the textfield and the port number on which the server is listening. This informatiion is visible to the person sitting on the server in a readonly text field which he will provide to the client. The client then can establish the connection with the server.
                      If and only if the server IP is accessible.
                      Now this could be the situation in which any computer could be server and any computer could be the client in the internet. So neither the server is predecided nor the client.
                      No it isn't.

                      There is absolutely no way that my home computer can operate as a server with someone on the internet acting as a client because my IP is not accessible from the internet.

                      There must be a public IP which is essentially under the control/domain of the server. This MIGHT exists via a router in a home system if one sets up the router for port forwarding. In a standard business set up the business will have a public IP and will set up their network infrastructure to route the requests correctly.
                      • 8. Re: Obtain IP Address
                        YoungWinston
                        aLkeshP wrote:
                        I'm developing an application which is based on server-client relation model wherein the client needs to put that ip address in the textfield and the port number on which the server is listening. This informatiion is visible to the person sitting on the server in a readonly text field which he will provide to the client. The client then can establish the connection with the server.
                        Now this could be the situation in which any computer could be server and any computer could be the client in the internet. So neither the server is predecided nor the client.
                        I'm no networking expert, but this had 'trouble' written all over it for me, and I'm glad to have it confirmed by a few others.

                        It sounds like a very clunky version of a passive connection (as with PASV ftp), and there may already be Java-based libraries around to do it at the socket level, automatically.

                        Basic idea is this:
                        1. Connect to server on a known port.
                        2. Server sends reply (secure or not) telling you which port to reply on (usually above 1024).
                        3. Send 'received' reply to known port, specifying the supplied port (optional).
                        4. Dialog proceeds via you and {server}:supplied-port#.
                        It's basically the Internet version of an old modem "callback".

                        Winston

                        Edited by: YoungWinston on Mar 5, 2011 11:25 PM
                        • 9. Re: Obtain IP Address
                          EJP
                          I'm developing an application which is based on server-client relation model wherein the client needs to put that ip address in the textfield and the port number on which the server is listening. This informatiion is visible to the person sitting on the server in a readonly text field which he will provide to the client. The client then can establish the connection with the server.
                          Only if the appropriate IP address can be found automatically and the appropriate port is open via all firewalls between the client and the server. In the most common case that is impossible, as the cheaper routers don't even have port forwarding.

                          Your application as you are presently designing it will not therefore work in the most common deployment cases. And in the other cases the customer at the server end will need sufficient network expertise to configure the port forwarding, in which case they will already know their own IP address. So what you are doing is infeasible in most cases and redundant in the others.