Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does RMI naming lookup work on the local host when I give it a garbage name?  RSS feed

 
James Marino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still new to RMI and I'm observing some strange behavior with RMI.
I'm using RMI as a communication protocol in a client/server database application. Since I don't have access to a network at the moment I was running the application on a single host and using the local host parameter to test my RMI code.

In my code if I do a naming lookup on the local host and it works as I would expect it to. (Note before this code fragment runs I've all ready started the rmi registry and bound the RemoteDatabase to it.)

Now if I replace "127.0.0.1" with a garbage value such as "12453" it still works!
This is not the behavior I expected. I expected that the Naming.lookup would throw a exception of some kind.
Can any one explain this behavior?
The reason I would like this to work as I expected is that in my application the IP address string and port number are variables that I allow the user to set through a dialog. If the user puts in a garbage string for the IP address I want to catch the exception and display an appropriate warning dialog to the user. I can't seem to do that since the Naming.lookup() for a garbage IP address doesn't seem to throw any exception that I have been able to detect.
I've actually observed even stranger behavior. If I use a garbage string such as 124.4.1.2, which is invalid IP address, the Naming.lookup() will throw an exception. If I put in a garbage string such as "aaaaaa" for the IP address the Naming.lookup() throws and exception. This is the behavior I would expect.
However as I mentioned if I use a string such as "12345" for the IP address the code works! This seemingly inconsistent behavior is driving me nuts so if any of you RMI gurus out there can help me out I would very much appreciate it.

Thanks in advance.
 
Rakesh Gadre
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello James,
I've tried your code, and to my surprise, it is giving an error (java.rmi.UnknownHostException ).
When I put IP of serveror localhost , it works fine, but if I replace it with the junk value, it throws an error mentioned above.
My code is as under :

INTERFACE
----------
import java.rmi.*;
public interface inter extends Remote
{
public String meth(String name) throws RemoteException;
}

SERVER PROGRAM
---------------
import java.rmi.*;
import java.rmi.server.*;
public class class1 extends UnicastRemoteObject implements inter
{
public class1() throws RemoteException
{
}
public String meth(String name) throws RemoteException
{
return "Hello "+name;
}

public static void main(String[] args)
{
try
{
class1 obj = new class1();
Naming.rebind("remotemethod",obj);
System.out.println("Server Ready");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
CLIENT PROGRAM
---------------
import java.rmi.*;
public class class2
{
public static void main(String argsp[])
{
try
{
String name = "rmi://" + "12.22.56.210" +":"+1099+"/remotemethod";
inter inte = (inter)Naming.lookup(name);
System.out.println(inte.meth("Rakesh"));
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Try this code
Rakesh
 
James Marino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply and I'm sorry I did not reply sooner but I had a crisis to deal with.
I see I was not clear enough in my initial problem statement. The key to reproducing the behavior I observed is to use an IP address of the form "12345". What the numbers are does not seem to matter but it is important that you do not put it in the form of 12.34.5.0.
I compiled and ran your code on my system and when I did I got the same results that you did when you used:
String name = "rmi://" + "12.22.56.210" +":"+1099+"/remotemethod";

However I changed your code to use a name string as shown below: (Notice the IP address is "12345".

String name = "rmi://" + "12345" +":"+1099+"/remotemethod";
inter inte = (inter)Naming.lookup(name);
System.out.println(inte.meth("Rakesh"));
then your client program automatically defaults to the local host rather than throwing an exception!
Now I also posted this my observations on this behavior on Sun's RMI forum on javasoft.com. I got a most excellent reply from a RMI guru.

Re: Bizzare behavior with RMI using the localhost
Author: nunoo
In Reply To: Re: Bizzare behavior with RMI using the localhost
Feb 25, 2003 4:38 PM
Reply 3 of 4
Hi, there.
lookup uses URI parsing so the fact that your using numbers being equivalent to localhost seems to be due to the fact that that is an invalid host according to the javadocs for the getHost primitive attached below(marked the most important parts). In this case returning null is interpreted as absent , therefore => localhost. Nothing about the port! Maybe you can take it from here. Seems you'll have to make some user input validation yourself, in your application. Hope I haven't missed anything.
Good luck.
Nuno
-----------------------------
getHost
public String getHost()Returns the host component of this URI.
The host component of a URI, if defined, will have one of the following forms:
A domain name consisting of one or more labels separated by period characters ('.'), optionally followed by a period character. Each label consists of alphanum characters as well as hyphen characters ('-'), though hyphens never occur as the first or last characters in a label.
********************************************************************
The last, or only, label in a domain name begins with an alpha character.
********************************************************************
A dotted-quad IPv4 address of the form digit+.digit+.digit+.digit+, where no digit sequence is longer than three characters and no sequence has a value larger than 255.
An IPv6 address enclosed in square brackets ('[' and ']') and consisting of hexadecimal digits, colon characters (':'), and possibly an embedded IPv4 address. The full syntax of IPv6 addresses is specified in RFC 2373: IPv6 Addressing Architecture.
The host component of a URI cannot contain escaped octets, hence this method does not perform any decoding.
Returns:
The host component of this URI, or null if the host is undefined.
*******************************************
I think that Naming.lookup() should throw an exception if I feed it a garbage string no matter what that string may contain. Perhaps I'm mistaken but I don't think Naming.lookup() should ever "automatically" default to local system.
I'm going to submit this to Sun as a java bug.
James
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!