Record 2:File transfer:
Create a server that asks for a password, then opens a file and sends the file over the network connection. Create a client that connects to this server, gives the appropriate password, then captures and saves the file.
-----------------------------------------------------------------------------------------
Program 1: Interface program filet.java
import java.rmi.*;
public interface fileT extends Remote
{
public String getfilepwd(String pwd) throws RemoteException;
}
Program 2: Implementation program fileTImp.java
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public class fileTImp extends UnicastRemoteObject implements fileT
{
private String fileurl;
public fileTImp(String fname) throws RemoteException
{
fileurl = fname;
}
public String getfilepwd(String pwd) throws RemoteException
{
if(pwd.compareTo("62075064")!=0) return "Invalid Password";
byte buffer[] = new byte[2056];
try
{
FileInputStream fis = new FileInputStream(fileurl);
int bytes = fis.read(buffer,0,2056);
try
{
String str = new String(buffer,0,bytes,"Default");
return(str);
}
catch(Exception e)
{ System.out.println("Error, maybe string error :"+e);
}
}
catch(Exception e)
{ System.out.println("Error at fileTImp : "+e);
}
return "File not found";
}
Program 3: Server program fileTServer.java
import java.rmi.*;
import java.rmi.server.*;
public class fileTServer
{
public static void main(String[] args)
{
try
{
System.out.print("Constructing Server Implementation...");
fileTImp fileT1 = new fileTImp("secret.txt");
Naming.rebind("secretfile",fileT1);
System.out.print("Done!nWaiting for Invocations from client...n");
}
catch(Exception e)
{ System.out.print("Error at fileTServer : "+e);
}
}
}
Program 4: Client program fileTClient.java
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public class fileTClient
{
public static void main(String[] args)
{
try
{
System.out.print("Enter Password : ");
DataInputStream dis = new DataInputStream(System.in);
String password = dis.readLine();
System.out.print("Naming.lookup for Bindings...");
fileT file1 = (fileT) Naming.lookup("secretfile");
System.out.print("Done!n");
System.out.print("Info from Server: n"+file1.getfilepwd(password));
}
catch(Exception e)
{ System.out.print("Error at fileTClient : "+e);
}
}
}
Program 5: Secrete file secrete.txt
!!! Congratulations !!!
You opened the file which requires secret password.
RPC = Remote Procedure Call
RMI = Remote Method Invocation
CORBA = Common Object Broker Architecture
ORB = Object Request Brokarage
IIOP = Internet Inter ORB Protocol
JDBC = Java DataBase Connectivity
Java IDL = Java Interface Definition Language
Author: Siva Ramaiah
E-mail: siva_sriramula@yahoo.com
Created:10:43 AM 11/12/2008
Program 6: Show Bindings showBingings.java
import java.rmi.*;
import java.rmi.server.*;
public class showBindings
{
public static void main(String[] args)
{
try
{
String[] bindings = Naming.list("");
for(int i=0;i<bindings.length;i++)
System.out.println("Binding ["+(i+1)+"] => "+bindings[i]);
}
catch(Exception e)
{ System.out.println("Error in showBindings : "+e.toString()); }
}
}
Program 7: clinet.policy clinet.policy
grant
{
permission java.net.SocketPermission
"*:1024-65335","connect";
};
1. compile all java files
javac *.java
2. use RMICompiler to create stubs & skeletons of Implementations
rmic fileTImp
3. run rmiregistry
start rmiregistry
4. create policy file with following contents
client.policy
~~~~~~~~~~~~~
grant
{
permission java.net.SocketPermission
"*:1024-65335","connect";
};
5. run server
java fileTServer
6. run client with policy created in step 4.
java -Djava.security.policy=client.policy fileTClient