Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

InetAddress and Java 1.3

Posted on 2001-08-15
7
Medium Priority
?
857 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Accepted Solution

by:
objects earned 200 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 200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

704 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