1

I have created a simple database application using rmi. It works fine with my local wireless network. But now i want to connect my client to the server through the internet. I know that, this can be achieved with setting up port forwarding in the router. But i want it to work in any computer which is connected to the internet using wifi connections, dialup connections etc. How to do that? what to write here? Naming.lookup ("rmi://?????????????"); As I am quite new to java, Please give me a detailed answer with a simple code example. Thanks in advance

user3641302
  • 149
  • 1
  • 2
  • 13
  • 3
    Your question is unclear, but I'm going to assume English isn't your first language. Java RMI is usually on [TCP port 1099](http://stackoverflow.com/q/3071376/2970947). Since RMI is on [TCP](http://en.wikipedia.org/wiki/Transmission_Control_Protocol), your question then becomes *how does [Internet routing work](http://en.wikipedia.org/wiki/Routing)?* – Elliott Frisch Jun 17 '14 at 13:29
  • I recommend you switch to using http-invoker or hessian instead of RMI. If you're using spring, it is quite simple. Http-invoker works over http/https on 80 or 443 port and usually don't required additional firewall settings on server and client. – user1516873 Jun 17 '14 at 13:49

1 Answers1

0

I hope you are messed up with Java RMI Concept. Irony is that a few days ago I was also thinking up the same except that I was thinking to connect up on my internal network.

There are two kinds of classes that can be used in Java RMI.

A Remote class is one whose instances can be used remotely. An object of such a class can be referenced in two different ways: 1. Within the address space where the object was constructed, the object is an ordinary object which can be used like any other object. 2. Within other address spaces, the object can be referenced using an object handle. While there are limitations on how one can use an object handle compared to an object, for the most part one can use object handles in the same way as an ordinary object.

A Serializable class is one whose instances can be copied from one address space to another. An instance of a Serializable class will be called a serializable object. In other words, a serializable object is one that can be marshaled.

SO, HERE COMES THE ANSWER TO YOUR QUESTION,ASSUMING YOU ARE TALKING ABOUT REMOTE CLASS ON DIFFERENT SYSTEM(SERVER).

The name of a remote object includes the following information:

The Internet name (or address) of the machine that is running the Object Registry with which the remote object is being registered. If the Object Registry is running on the same machine as the one that is making the request, then the name of the machine can be omitted.

The port to which the Object Registry is listening. If the Object Registry is listening to the default port, 1099, then this does not have to be included in the name.

The local name of the remote object within the Object Registry.

The URL for a remote object is specified using the usual host, port and name:

rmi://host:port/name

host = host name of registry (defaults to current host) port = port number of registry (defaults to the registry port number) name = name for remote object

Assuming that your code is lying on the server having host-name as "XYZ.edu/home/CLasses"(you can give the DNS/IP-Address of server and include the location of the Class file),port-number="1099"(default) and name of the remote Object="abc" for your ABC.java Class in Server. In this way one will be able to call the Remote Object from different machines. Also, you need to keep the whole Server code on the Internet Address so that Clients can access them from the Internet(one can't access the offline-code present in your machine). Then only it can happen!!!

Here is the example Client program:

/**
* Client program for the "Hello, world!" example.
* @param argv The command line arguments which are ignored.
*/
public static void main (String[] argv) {
try {
  HelloInterface hello = 
    (HelloInterface) Naming.lookup ("//ortles.ccs.neu.edu/Hello");   //see here the address of the server hosting the Server file,you can omit port number,it'll take default port 1099.
  System.out.println (hello.say());
 } catch (Exception e) {
  System.out.println ("HelloClient exception: " + e);
 }
}
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • According to you, it is not possible to connect through internet, but i have connected using my wireless network where the server is running on one computer and client is running on another computer, so why it is not possible through the internet? At the end internet is a network! – user3641302 Jun 17 '14 at 15:50
  • @user3641302-You misinterpreted my answer,I didn't say so. Of course,you can do so using Internet. But,you need to have the Server files residing on Server IP Address,and then one can use remote Objects as client to connect to them. What you have achieved is that I already mentioned,I also tried the same and was successful. You need server IP address to connect to access the source files to `bind()` and `rebind()`. Please go through the answer once again! – Am_I_Helpful Jun 17 '14 at 16:11
  • Obviously,you need to host up files at some IP Address,it's not that you will directly able to connect. How did you connect it in your personal network? How did you connect it to your local server using WiFi setup and all? Please explain so that I can explain you the rest and clear your doubt!!! – Am_I_Helpful Jun 17 '14 at 16:15
  • i used the ip address of the computer which was running the server in my client app,("rmi://ip address of the server computer:1099/Hello"); The problem is when i use the public ip address of the server computer i'm unable to connect. As i said i'm quite new to programming so please be kind enough to give me some detailed answer.Tnx – user3641302 Jun 17 '14 at 17:01
  • The exception i'm getting is java.rmi.ConnectException: Connection refused to host:ip address.Nested exception is ava.net.ConnectException: Connection timed out: connect. – user3641302 Jun 17 '14 at 17:26
  • Check this link---http://www.eg.bucknell.edu/~cs379/DistributedSystems/rmi_tut.html. I hope it answers your question. Also,for your exception search on the internet more and more. I already mentioned that you'll mess up.Try connecting the client too to the network and then connect to the machine(server). – Am_I_Helpful Jun 17 '14 at 18:07