Solved

InetAddress and Java 1.3

Posted on 2001-08-15
7
838 Views
Last Modified: 2008-02-01
Hi,
I am developing a Applet and i want to get the host name of the computer i an running the applet on.
When i use JApplet.getCodeBase().getHost() I receive localhost instead of the server name.
When i use InetAddress.getLocalHost().getHostName() i get the same.
Any ideas ???
Thanx.
0
Comment
Question by:rotem_fo
7 Comments
 
LVL 4

Expert Comment

by:antons061400
ID: 6389225
probably you download that applet from your PC. (a web server take it from there)
0
 
LVL 92

Expert Comment

by:objects
ID: 6390532
Firstly the two thing you are try are different:

JApplet.getCodeBase().getHost()
Returns the host of the server where the applet was loaded.

InetAddress.getLocalHost().getHostName()
Returns the hostname of the client. This information is not accessible to unsigned applets and thus it is returning 'localhost' You need to sign the applet to be able to find the local hostname.
0
 

Author Comment

by:rotem_fo
ID: 6391439
Hi,
I used getCodeBase().getHost() .
Whenever i type the server's name in the address bar (in the internet explorer) i receive the machine name, if i type the ip address the getCodeBase().getHost() returns the ip entered in the address bar.
I am lost :(

R.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6391482
Yes, getHost() returns the host part of the url, which is the ip address if that's what you've entered, or the hostname if you used that.
0
 
LVL 1

Assisted Solution

by:dviji
dviji earned 50 total points
ID: 6392827
You can get the HostName even you type ipaddress...
By using InetAddress.getByName(String) & InetAddress.getAllByName(String) methods.

i.e
 String hostName = getCodeBase().getHost();
 InetAddress inetAddress = InetAddress.getByName(hostName);
 inetAddress.getHostName();
 inetAddress.getHostAddress();

getByName() method will give you an InetAddress object. The input parameter must be string (if it is ipaddress or name that is not a matter).

Some hosts (most of the popular portals) may have many ipaddress (that is for load balancing) with a single name. In that case you can use getAllByName().

So the better solution is using getAllByName()... it will return an array of InetAddress...From that you can get the values.

ie. Your should looks like..

 String hostName = getCodeBase().getHost();
 InetAddress[] inetAddresses[] = InetAddress.getAllByName(hostName);

 for(int i=0;i<inetAddresses.length;i++)
 {
   inetAddresses[i].getHostName();
   inetAddresses[i].getHostAddress();
 }


...dviji


 

0
 

Expert Comment

by:CleanupPing
ID: 9061860
rotem_fo:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 9330425
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between objects and dviji

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now