aspose file tools*
The moose likes Applets and the fly likes Local host diffrence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Local host diffrence" Watch "Local host diffrence" New topic
Author

Local host diffrence

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi,
I have a code in my applet
String ip="";
String host="";
InetAddress ina=InetAddress.getLocalHost();
ip=ina.getHostAddress();
host=ina.getHostName();
And I am printing it in applet in the
public void paint ()
method....
Problem is:
Running locally, it picks my ip (through which I am connected) and my computer name but when I access it remotely, it just shows "127.0.0.1" and "localhost"
Why this behaviour?
Thanx in advance
Maki Jav


Help gets you when you need it!
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Maki
When you say "locally", what you mean exactly? Are you running the applet view Appletviewer OR the browser on the local machine?
Thanks!
Maulin
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Maulin
I have an applet on my computer and I have accessed it through a browser and through appletviewer. The result is the same. No problem there.
If I am connected to internet, I get the ip address that the ISP has assigned me, for instance I am getting 62.145.90.001 right now, and 127.0.0.1 when I am logged off. I get the correct name of my computer in both the conditions.
Now that is perfectly fine....
However,
When I take the applet and it's html file to a web site say geocities.com, I get 127.0.0.1 for ip and localhost for computer name all the time
Thanx in advance
I can give you the link if you are interested to see it for yourself.
Thanx in Advance
Maki Jav
[ October 12, 2003: Message edited by: Maki Jav ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Maki
I figured out the problem but only partially I would say.
Here is a excerpt from the Java 1.4 API's InetAddress's getLocalHost() method,

If there is a security manager, its checkConnect method is called with the local host name and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, an InetAddress representing the loopback address is returned.

So, I did created an applet that is using getLocalHost() and confirmed the same behavior you get via uploading my applet to geocities. Then, I modified my java.policy file to allow "AllPermissions" for the codebase,
http://www.geocities.com/maulin_v
and then it worked for me. You can see the applet at,
http://www.geocities.com/maulin_v/TestApplet1.html
If we concentrate on last part of what API has to say in above excerpt , I thought it is because somehow the applet code is not allowed permission to "resolve" on local host which seemed to be the reason...
Here, I failed to find specific settings that we have to make in java.policy to make this work. I had to go with "AllPermissions" thing but may be somebody else can point specific permission to set in java.policy rather than AllPermissions...
Regards
Maulin
kjs
Greenhorn

Joined: Oct 10, 2003
Posts: 7
Hi ,
I have been tryin desperately as it is very to get this thing up for 3 days now.I have posted this Q, in almost all the forums, have got similar replied with adjusting the java.policy file, and also using the policy tool.
But it doesnt seem to work alright.I have the localhost stuff haunting me.
Is there any browser setting i have to take care or import this policy file anywhere.
Thanks in advance...
kjs
Greenhorn

Joined: Oct 10, 2003
Posts: 7
Hi,
I got this link from one off my post
http://www.jguru.com/faq/view.jsp?EID=15832
this seemed to work, actually connecting back to the server and then the server recogonizes the IP address.
But donno how correct this approach is.
the java policy stuff failed.....
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi Sajit
the best way to set policy file is to modify/add the ".java.policy" file in the user's home directory to avoid tracking of latest active JRE running. put .java.policy file in the user's home directory and then see if it works...
you can read more at here
thanks
maulin
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Thanks sajit and Maulin
I think that going the sajit's way is okay if you don't want to have any of your .java.policy file at the client machine.
And as far Maulin your approach is concerned, it is also useful in situations where the user allows you the have more access to his system.
but Maulin I could not reach your link
http://www.geocities.com/maulin_v/TestApplet1.html
ie it shows nothing informative just the information that you have an applet on it
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Maki
You can look at the "Java console" to see the output I didn't put anything on the applet so you would get gray area...
I'm lazy when I write code for little testing..
Thanks!
Maulin
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Maulin,
This is what Java console has to tell me about your applet's work
Codebase:http://www.geocities.com/maulin_v/
Host address:127.0.0.1
Host name:localhost

Same problem that I was facing? right?
Maki Jav
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
yes. You have to set .java.policy (please observe the . in the beginning of the filename. its different than java.policy that is there in JAVA_HOME/jre/lib/security) in your user home directory to have AllPermissions for this codebase http://www.geocities.com/maulin_v and then it would work...
Though I like that JGuru link's suggestion to use Socket to connect to the orginating server...
Thanks
Maulin
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
sajit and Maulin
would you check this link
It was uploaded by me so I can give you answers. I can send you the Applet code if you like
it has a .java.policy file which is zipped and you have to download it
after which you may be able to see applet in action
As both of you are programmers so I hope that you can read and understand my .java.policy file and what rights it has given to the applet
this link is
http://www.geocities.com/adilsec
one funny thing is that came up is that I went to a cyber cafe to check it and the applet gave me the error .... unknown host : www.geocities.com
hahahahahaha
anyways,
Thanks in advance
Maki Jav
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi,
The problem which I came up has mostly solved. I can now get the ip address of the client on which my page containing the my applet, is opened.
I used this code:
URL url = getDocumentBase();
String host = url.getHost();
Socket socket = new Socket(host, 8080);
InetAddress addr = socket.getLocalAddress();
ip = addr.getHostAddress();
try{
host=addr.getHostName();
}catch(Exception e){
host=e.getMessage();
//host=host.substring(18);
host=host.trim();
}
It seems that you cannot collect the name of the host. When I had not caught the exception here, I was getting message like :
Cannot connect to "star"
So there must be some way to have host name in addition to ip address (which I am getting, without any problem, using the above code) using the above code. Somehow I have missed it!?
Thanx in Advance,
Maki Jav
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
sajit
my applet with the method getDocumentBase() is working fine when I run it from command prompt.
You should check your code again
Maki Jav
[ October 16, 2003: Message edited by: Maki Jav ]
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi,
What we have been discussing can be seen in action at
http://www.geocities.com/adilsec/TestIO.html
You will see your ip address
but you will see your computer name ie host as localhost
Need source code of the applet?
Just ask!

Maki Jav
[ October 17, 2003: Message edited by: Maki Jav ]
kjs
Greenhorn

Joined: Oct 10, 2003
Posts: 7
Hi maki and maulin,
Yeah Thank U, I finally got both the getDocumentBase() and the .java.policy file solutions for this problem working.
But the draw back for getDocumentBase(),if your try to run the application without downloading from webserver, it throws an acception which should be caught and handled so that it connects locally.
But is it a wise idea to make connections, and is it possible for clients to restrict making a new connections.Like that at the internet parlour i suspect, the proxy would not have allowed(just speculating lol).
Maki&Maulin, is there any we can download the .java.policy file into the client machine. Else it would be inconvinient for them to do this each time.
kjs
Greenhorn

Joined: Oct 10, 2003
Posts: 7
The exception it throws, when run locally without ,from command prompt is
a null Pointer ,cause we are try to retrieve the URL.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
.java.policy needs a single download only. Why download it everytime?
Maki Jav
[ October 29, 2003: Message edited by: Maki Jav ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local host diffrence