Archived

This topic is now archived and is closed to further replies.

utopico

[java] RMI and connections

Recommended Posts

utopico    122
I am writing a server - client application with RMI. Things are more or less running, but there are a lot of things I''m not so sure about. Here thay come. When using UnicastRemoteObject and I create a server an instance of this, will diffrent clients be able to connect simultaniously without interfereing with each others data? If not how should this be done? I have tried to make a remote login method on the this object that will return another Remote object, but I couldn''t make it work. I found a solution where this remote object also inherited from UnicastRemoteObject, but is this realy nessecarry? When I have have this object, it will not be available after some time. Looks like if the garabage collector throws it away some time after it has been used for the first time. So what I did was to hava e thread holding the reference to the object. Seemed to me as a realy nasty solution... The default for the UnicastRemoteObject is to lett a random port be made when connecting, this is not acceptable for me because I have to run this through a firewall with only special ports enabled. Prefereabely as few ports as possible (1 for RMI and one for the server, is this possible)? I have been looking around on the net to find solution to my problems but either there are typically whitepapers of 100pages+ or there are very simple tutorials on how to make RMI server/clients. So can you please help me. Thanks in advance.

Share this post


Link to post
Share on other sites
_walrus    126
"When using UnicastRemoteObject and I create a server an instance of this, will diffrent clients be able to connect simultaniously without interfereing with each others data? If not how should this be done?"

Your remote objects (Impl) should extend the UnicastRemoteObject and implement an interface you defined (that extends Remote). After you bind this object to the namingservice a client can resolve it, given they know have the name, thus multiple clients will be able to aquire the reference to this object and make invocations on the methods. You have to worky about simutanious invokations of a method, for instance, if to clients attempt to withdraw 50 dollars at the same time, and there is only 50$ in the account, there will be problems....You should beable to use the syncronization feature of java to help solve this.

Your server should keep a reference to your (Servants) Impl so the gc won''t clean it up. it should also bind them with the namingserver after instantiating them.

"I have tried to make a remote login method on the this object that will return another Remote object, but I couldn''t make it work."

Your clients can aquire the loginImpl object or what ever you want to call it. after successfuy authenication it should return a reference of another servant (gameImpl or whatever). Client can then invoke the methods defined in the interface that this servant implements.



The following is a great tutorial on how to get started with RMI.
http://java.sun.com/j2se/1.3/docs/guide/rmi/getstart.doc.html

Your best bet is to understand the simple tutorials and understand them thoroughly; try adding some features to them and once you undersand these go on and employ your knowedge, it''s better than building/fixing/learning all at once.



Hope this helps

Share this post


Link to post
Share on other sites
utopico    122
Thanks for your reply, I''v now got it to work almost. I can have a lot of clients connected, but the probleme arisis when recycling the ports used. As mentioned I would like to limit the number of ports used so what I try to do is to have list of possible port and register/unregister when each of the loginObjects are created and destroyed. This works almost fine. The problem is that when a client tries to reconnect on a port that has been previously in use I get an exception:

java.rmi.server.ExportException: Port already in use: 1105; nested exception is:
java.net.BindException: Address already in use

and when I try to list ports that are active :
netstat -a|grep 11
tcp 0 0 *:1105 *:* LISTEN

So it is obviously not free. How can I free this port for use for other clients?

Share this post


Link to post
Share on other sites