by
Irving Salisbury III (Author of Advanced Java Programming)
irving.salisbury@East.sun.com
and
rinaldo.digiorgio@East.sun.com
There is an interface called FileReceiver that implements the java.rmi.Remote interface which is required for remote objects needing to respond to RMI requests. The class that actually implements the remote object is called RemoteReceiver. It implements the FileReceiver interface and also extends the UnicastRemoteObject class, which aids in setting up the RMI object. When started, this class first creates a RemoteReceiver object, then binds that to the name receiver. This is the name that the client must know in order to connect to it.
To transfer a file, a client connects to the RemoteReceiver using the registry services of RMI and then calls receiveFile, passing in a FilePacket object. FilePacket is an object that implements Serializable, which means it will be passed by value, that is the object will be converted to a bitstream and sent by the underlying RMI subsystems from the client to the server. The RemoteReceiver then tells the FilePacket object it just received to write itself out to a file with the same name and a .snt extension. FilePacket knows how to read itself in from a file name, and can write itself out to any OutputStream.
On the sending side, the FileSender class first creates an object of FileSender in thejava application. This example is not an applet but could be converted to one with the standard security restrictions. For example you would not be able to read a file with Netscape 3.0.1 but you would be able to do that with the appletviewer as discussed earlier. In the constructor for FileSender, the remote object named receiver is first found on the hostname that was passed in on the command line. Then, a Frame is created with one java.awt.button. When that button is pressed, a FileDialog is created. After the dialog closes, the filename is extracted from the FileDialog and a FilePacket object is made from the name. The FilePacket is told to read itself in, and then passed to the remote object. The transfer is done automatically through RMI. That's all there is to it. To run this exampe untar or unzip:
To install and use this example please follow the following steps. Please use the JDK1.1.1 for these examples since this is an application and not an applet.
You must start up your rmiregistry on the appropriate machine, making sure to set the CLASSPATH so it can get at the skeleton and stub files.
or
Then, just start up the RemoteReceiver
Then, start up the client side on a machine