Articles‎ > ‎Technical Articles‎ > ‎

RMI File Downloading Program

posted Aug 13, 2009, 2:14 AM by Thiyagaraaj M   [ updated Dec 1, 2011, 5:00 AM by Thiyagaraaj Mp ]

(Remote Method Invocation) A standard from Sun for distributed objects written in Java. RMI is a remote procedure call (RPC), which allows Java objects (software components) stored in the network to be run remotely. Unlike CORBA and DCOM objects, which can be developed in different languages, RMI is designed for objects written only in Java.

Introduction

Remote method invocation allows applications to call object methods located remotely, sharing resources and processing load across systems. Unlike other systems for remote execution which require that only simple data types or defined structures be passed to and from methods, RMI allows any Java object type to be used - even if the client or server has never encountered it before. RMI allows both client and server to dynamically load new object types as required. In this article,have simple RMI Program

Interface

import java.rmi.*;
public interface StackInter extends Remote
{
byte[] st(String str)throws RemoteException;
}

Implementation


import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
import java.util.*;

public class StackImpl extends UnicastRemoteObject implements StackInter
{
public StackImpl() throws RemoteException {}
public byte[] st(String str)throws RemoteException
{
try
{
File f=new File(str);
byte b[]=new byte[(int)f.length()];
BufferedInputStream f1=
new BufferedInputStream(new FileInputStream(f));
f1.read(b,0,b.length);
f1.close();
return(b);
}
catch(Exception e) { }
}
}

Server

import java.rmi.*;
import java.net.*;
public class StackServer
{
public static void main(String arg[])
{
try
{
StackImpl im=new StackImpl();
Naming.rebind("hai",im);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Client


import java.io.*;
import java.rmi.*;
import java.rmi.server.*;

public class NormalClient
{ public static void main(String[] arg)
{
String IpName,FileName,Ip;
StackInter DataObject;
byte[] Content;
try { BufferedReader Buffer=new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Ip Address :");
IpName = Buffer.readLine();
System.out.println("Enter the File Name :");
FileName = Buffer.readLine();
Ip="rmi://"+IpName+"/hai";
DataObject=(StackInter)Naming.lookup(Ip);

Content = DataObject.st(FileName);
File Fn=new File(FileName);
BufferedOutputStream OutFile=new BufferedOutputStream(new FileOutputStream(Fn.getName()));
OutFile.write(Content,0,Content.length);
OutFile.flush();
OutFile.close();
}
catch(Exception e){}
}
}



More RMI Concepts
Learn Java RMI
Introduction to Java RMI,RMI Overview,Writing an Interface,Implementing the interface,Writing RMI Client,and Running the Client and Server.