LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-20-2011, 07:37 AM   #1
kapz_unlocked
LQ Newbie
 
Registered: Jun 2011
Distribution: CentOS 5.5
Posts: 11

Rep: Reputation: Disabled
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!
 
Old 06-20-2011, 08:46 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,834
Blog Entries: 14

Rep: Reputation: 1081Reputation: 1081Reputation: 1081Reputation: 1081Reputation: 1081Reputation: 1081Reputation: 1081Reputation: 1081
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
 
1 members found this post helpful.
Old 06-20-2011, 10:26 AM   #3
kapz_unlocked
LQ Newbie
 
Registered: Jun 2011
Distribution: CentOS 5.5
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks a lot buddy!! There seems some light in those links.
I'll try them tomorrow and see..
 
Old 06-21-2011, 02:58 AM   #4
kapz_unlocked
LQ Newbie
 
Registered: Jun 2011
Distribution: CentOS 5.5
Posts: 11

Original Poster
Rep: Reputation: Disabled
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;
	}
 }
 
1 members found this post helpful.
Old 06-21-2011, 11:54 AM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,834
Blog Entries: 14

Rep: Reputation: 1081Reputation: 1081Reputation: 1081Reputation: 1081Reputation: 1081Reputation: 1081Reputation: 1081Reputation: 1081
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.
 
1 members found this post helpful.
Old 10-08-2012, 11:21 AM   #6
xehpuk
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux (in "Cloud") to Linux (local LAN) tunnel for multiple local printers. motoslide Linux - Networking 5 10-28-2010 06:20 PM
Can I (non-root) change the default Java for just my account to my own local java? SteveTheRed Linux - Software 1 11-11-2009 11:51 AM
[SOLVED] Can launch Java webapp from rc.local navigat0 Linux - Server 2 07-01-2009 10:48 AM
java app in local zone SLOOOOOW Tinkster Solaris / OpenSolaris 13 02-23-2007 02:18 AM
symbolic link between mozilla plugins and /usr/local/java TSynergy Linux - Newbie 3 04-15-2006 03:17 PM


All times are GMT -5. The time now is 10:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration