LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Get the local IP using Java on Linux (https://www.linuxquestions.org/questions/linux-newbie-8/get-the-local-ip-using-java-on-linux-887319/)

kapz_unlocked 06-20-2011 07:37 AM

Get the local IP using Java on Linux
 
Hi,
I am trying to do FTP file transferring using java language.
Found a code from a site and trying to modify it as I need. It works fine on Windows but I need on Linux (I'm working on CentOS 5.5).
When I run it on Linux it doesn't success, as I found because it gets the local IP as 127.0.0.1. (but in Windows it get the real IP)

The code part that gets the IP:

Code:

private static boolean openPort(ServerSocket serverSocket) throws IOException
        {                       
                int localport = serverSocket.getLocalPort();

                // get local ip address
                InetAddress inetaddress = serverSocket.getInetAddress();
                InetAddress localip;
               
                java.net.InetAddress addr = java.net.InetAddress.getLocalHost();
                try {
                        localip = inetaddress.getLocalHost();
                } catch(UnknownHostException e) {
                        debugPrint("Can't get local host");
                        return false;
                }

                ....

                }

Here the 'localip' variable gets 127.0.0.1.
Can somebody help me to get the real IP of the PC? (172.16.102.xxx)
Really appreciate your helps. :)
Thanks in advance!

MensaWater 06-20-2011 08:46 AM

Windows is broken because "localhost" is ALWAYS 127.0.0.1.

There is discussion about your question at the following link. Haven't tried this myself but it looks promising:
http://www.jguru.com/faq/view.jsp?EID=15835

kapz_unlocked 06-20-2011 10:26 AM

Thanks a lot buddy!! There seems some light in those links.
I'll try them tomorrow and see.. :)

kapz_unlocked 06-21-2011 02:58 AM

Thanks to the link given by MensaWater I was able to solve the problem. But had to use another funtion to get the local IP.
Thought of adding it here in case any body else get the same issue some day:

Code:

import java.net.*;
import java.util.*;

import java.util.regex.Pattern;

public class GetPublicHostname
{
        public static void main(String[] args) throws Throwable
        {
                System.out.println("The IP : " + tellMyIP());
        }

        public static String tellMyIP()
        {
                NetworkInterface iface = null;
                String ethr;
                String myip = "";
                String regex = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
                try
                {
                        for(Enumeration ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();)
                        {
                                iface = (NetworkInterface)ifaces.nextElement();
                                ethr = iface.getDisplayName();

                                if (Pattern.matches("eth[0-9]", ethr))
                                {
                                        System.out.println("Interface:" + ethr);
                                        InetAddress ia = null;
                                        for(Enumeration ips = iface.getInetAddresses();ips.hasMoreElements();)
                                        {
                                                ia = (InetAddress)ips.nextElement();
                                                if (Pattern.matches(regex, ia.getCanonicalHostName()))
                                                {
                                                        myip = ia.getCanonicalHostName();
                                                        return myip;
                                                }
                                        }
                                }
                        }
                }
                catch (SocketException e){}
                return myip;
        }
 }


MensaWater 06-21-2011 11:54 AM

Glad you got it resolved. Can you go to Thread Tools at top of page and mark it solved please? That way others having the same problem will see there is a solution in their web searches.

xehpuk 10-08-2012 11:21 AM

Hi,

Thanks for the solution.

To make it work also on a laptop I changed on line, I added "wlan".

if ((Pattern.matches("eth[0-9]", ethr)) || (Pattern.matches("wlan[0-9]", ethr)))
/xehpuk


All times are GMT -5. The time now is 11:32 AM.