To Create
a program to download files from various servers using RMI
- Define the interface.
- Interface must extend Remote
- All methods must throw an exception Remote Exception individually
- Implement the interface.
- Create a server program.
- Create a Client Program.
- Generate stubs and skeletons using rmic tool.
- Start the server.
- Start the client.
- Once the process is over stop the client and server respectively.
fileinterface.java
// Comment import java.rmi.*; public interface fileinterface extends Remote { public byte[]download file(String s)throws RemoteException; }
fileimplement.java
// Comment import java.io.*; import java.rmi.*; import java.rmi.server.*; public class fileimplement extends UnicastRemoteObject implements fileinterface { private String name; public fileimplement(String s)throws RemoteException { super(); name=s; } public byte[]downloadfile(String fn) { try { File fi=new File(fn); byte buf[]=new byte[(int)fi.length()]; BufferedInputStream bis=new BufferedInputStream(new FileInputStream(fn)); bis.read(buf ,0,buf.length); bis.close(); return(buf); } catch(Exception ee) { System.out.println("Error:"+ee.getMessage()); ee.printStackTrace(); return(null) ; }} }
fileserver.java
// Comment import java.rmi.*; import java.io.*; import java.net.*; import java.rmi.registry.*; public class fileserver { public static void main(String arg[]) { try { fileimplement fi=new fileimplement("fileserver"); Registry reg=LocateRegistry.createRegistry(5000); reg.rebind("fileserver",fi); System.out.println("server is ready"); } catch(Exception e) { System.out.println(" "+e.getMessage()); e.printStackTrace(); } } }
fileclient.java
import java.net.*; import java.rmi.*; import java.io.*; public class fileclient { public static void main(String[] args) { try{ // InetAddress addr=InetAddress.getLocalHost(); // String address=addr.toString().substring(addr.toString().indexOf("/")+1); // String url="rmi://"+ address + "/fileserver"; fileinterface f1=(fileinterface)Naming.lookup("rmi://localhost:5000/fileserver"); byte[] data=f1.downloadfile(args[0]); File ff=new File("f1.txt"); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(ff.getName())); bos.write(data,0,data.length); bos.flush(); bos.close();} catch(Exception e){ System.out.println(" "+e.getMessage()); e.printStackTrace(); } } }
Output Screen
No comments:
Post a comment