Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
rmi-steps21
1. Server Side:
1 Let�s create a new Java Project using Eclipse ( or NetBeans or other editor you
prefer), and call it : SimpleChatServer-> Click Finish once done
2 Under the SimpleChatServer project, Select New -> Interface, set the name for the
interface as : ChatInterface. The Java code for the interface is as below:
import java.rmi.*;
import java.rmi.*;
import java.rmi.server.*;
Naming.rebind("rmi://localhost/ABC", server);
while(true){
String msg=s.nextLine().trim();
if (server.getClient()!=null){
ChatInterface client=server.getClient();
msg="["+server.getName()+"] "+msg;
client.send(msg);
}
}
}catch (Exception e) {
System.out.println("[System] Server failed: " + e);
}
}
}
5 Compile your program.
6 Run the rmic from the console ( cmd ) to generate the stub for the Chat class.
7 Create the policy file for the security with the following code:
grant {
permission java.security.AllPermission;
};
8 Set the VM Arguments for your project using the following :
-Djava.security.policy=file:${workspace_loc}/SimpleChatServer/security.policy
-Djava.rmi.server.codebase=file:${workspace_loc}/SimpleChatServer/bin/
9 Once your Run your server, you will be asked to type in your name as shown below:
rmi-steps19
2. Client Side:
1 Let�s create a new Java Project using Eclipse and call it : SimpleChatClient->
Click Finish once done
2 Under the SimpleChatClient project, Select New -> Class, set the name for the
class as : ChatClient. The Java code for the class is as below:
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class ChatClient {
public static void main (String[] argv) {
try {
System.setSecurityManager(new RMISecurityManager());
Scanner s=new Scanner(System.in);
System.out.println("Enter Your name and press Enter:");
String name=s.nextLine().trim();
ChatInterface client = new Chat(name);
ChatInterface server =
(ChatInterface)Naming.lookup("rmi://localhost/ABC");
String msg="["+client.getName()+"] got connected";
server.send(msg);
System.out.println("[System] Chat Remote Object is ready:");
server.setClient(client);
while(true){
msg=s.nextLine().trim();
msg="["+client.getName()+"] "+msg;
server.send(msg);
}
}catch (Exception e) {
System.out.println("[System] Server failed: " + e);
}
}
}
3 Exactly the same way as the server side, create the interface : ChatInterface,
class: Chat, Policy: security.policy within the Client Project.