Sei sulla pagina 1di 3

Java RMI Example : Simple Chat Program between Server and ClientPosted by Imed

Bouchrikaon October 29, 2013 in Java RMI, Programming16 Comments


In case you are new to Java RMI, You need to follow this simple tutorial to learn
RMI
In this tutorial, we will create a simple chat program using two users. One user
will be chatting from the server side, whilst the other user would be chatting from
the client side of the RMI application. The structure of the files for the projects
created using Eclipse throughout this tutorials is shown below:

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.*;

public interface ChatInterface extends Remote{


public String getName() throws RemoteException;
public void send(String msg) throws RemoteException;
public void setClient(ChatInterface c)throws RemoteException;
public ChatInterface getClient() throws RemoteException;
}
3 Under the SimpleChatServer project, Select New -> Class, set the name for the
class as : Chat. The Java code for the class is as below:

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

public class Chat extends UnicastRemoteObject implements ChatInterface {

public String name;


public ChatInterface client=null;

public Chat(String n) throws RemoteException {


this.name=n;
}
public String getName() throws RemoteException {
return this.name;
}

public void setClient(ChatInterface c){


client=c;
}

public ChatInterface getClient(){


return client;
}

public void send(String s) throws RemoteException{


System.out.println(s);
}
}
4 Under the SimpleChatServer project, Select New -> Class, set the name for the
class as : ChatServer. The Java code for the class is as below:
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;

public class ChatServer {


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();

Chat server = new Chat(name);

Naming.rebind("rmi://localhost/ABC", server);

System.out.println("[System] Chat Remote Object is ready:");

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.

4 You can run the RMIC from the client also.

5 Set the VM Arguments for the client as :


-Djava.security.policy=file:${workspace_loc}/SimpleChatClient/security.policy
-Djava.rmi.server.codebase=file:${workspace_loc}/SimpleChatClient/bin/
6 Run the client side and start Chatting !

Potrebbero piacerti anche