Solved

InetAddress and Java 1.3

Posted on 2001-08-15
7
837 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stringclean challenge 26 56
for i loop in grovy 1 32
JDeveloper 12c for 32 bit 4 35
JList custom Cell Renderer refresh 15 43
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

760 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

20 Experts available now in Live!

Get 1:1 Help Now