Solved

InetAddress and Java 1.3

Posted on 2001-08-15
7
841 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

830 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