1. ChatInterface - Which Is A Interface Common For Both Client and Server.
2. Server - Which Implements The RMI Server
3. Client - Which Implements The Client.
NOTE : Server Is Initially Hard Coded With Five Client Names and Password. In Order To Do That I Have Used Two String Arrays. Their Are Lot Of More Easy Ways To Implement The Logging Scenario. If You Want You Can Use A Database or Separate File Store Client Details.
ChatInterface.java
1 2 3 4 5 6 7 | import java.rmi.RemoteException; public interface ChatInterface extends java.rmi.Remote { boolean checkClientCredintials(ChatInterface ci,String name, String pass) throws RemoteException; void broadcastMessage(String name,String message) throws RemoteException; void sendMessageToClient(String message) throws RemoteException; } |
Server.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import java.net.MalformedURLException; import java.util.ArrayList; public class Server extends UnicastRemoteObject implements ChatInterface { private static final long serialVersionUID = 1L; private String clientName [] = {"Nifal","Dinesh","Asjad","Clarry","Tahar"}; private String clientPass [] = {"12345","123456","12345","123456","12345"}; private ArrayList<ChatInterface> clientList; protected Server() throws RemoteException { clientList = new ArrayList<ChatInterface>(); } public synchronized boolean checkClientCredintials(ChatInterface chatinterface,String clientname,String password) throws RemoteException { boolean chkLog = false; for(int i=0; i<clientName.length; i++) { if(clientName[i].equals(clientname) && clientPass[i].equals(password)) { chkLog = true; this.clientList.add(chatinterface); } } return chkLog; } public synchronized void broadcastMessage(String clientname , String message) throws RemoteException { for(int i=0; i<clientList.size(); i++) { clientList.get(i).sendMessageToClient(clientname.toUpperCase() + " : "+ message); } } public synchronized void sendMessageToClient(String message) throws RemoteException{} public static void main(String[] arg) throws RemoteException, MalformedURLException { Naming.rebind("RMIServer", new Server()); } } |
Client.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import java.net.MalformedURLException; import java.util.Scanner; public class Client extends UnicastRemoteObject implements ChatInterface , Runnable { private static final long serialVersionUID = 1L; private ChatInterface server; private String ClientName; boolean chkExit = true; boolean chkLog = false; protected Client(ChatInterface chatinterface,String clientname,String password) throws RemoteException { this.server = chatinterface; this.ClientName = clientname; chkLog = server.checkClientCredintials(this,clientname,password); } public void sendMessageToClient(String message) throws RemoteException { System.out.println(message); } public void broadcastMessage(String clientname,String message) throws RemoteException {} public boolean checkClientCredintials(ChatInterface chatinterface ,String clientname,String password) throws RemoteException { return true; } public void run() { if(chkLog) { System.out.println("Successfully Connected To RMI Server"); System.out.println("NOTE : Type LOGOUT to Exit From The Service"); System.out.println("Now Your Online To Chat\n"); Scanner scanner = new Scanner(System.in); String message; while(chkExit) { message = scanner.nextLine(); if(message.equals("LOGOUT")) { chkExit = false; } else { try { server.broadcastMessage(ClientName , message); } catch(RemoteException e) { e.printStackTrace(); } } } System.out.println("\nSuccessfully Logout From The RMI Chat Program\nThank You For Using...\nDeveloped By Nifal Nizar"); } else { System.out.println("\nClient Name or Password Incorrect..."); } } public static void main(String[] args) throws MalformedURLException,RemoteException,NotBoundException { Scanner scanner = new Scanner(System.in); String clientName = ""; String clientPassword = ""; System.out.println("\n~~ Welcome To RMI Chat Program ~~\n"); System.out.print("Enter The Name : "); clientName = scanner.nextLine(); System.out.print("Enter The Password : "); clientPassword = scanner.nextLine(); System.out.println("\nConnecting To RMI Server...\n"); ChatInterface chatinterface = (ChatInterface)Naming.lookup("rmi://localhost/RMIServer"); new Thread(new Client(chatinterface , clientName , clientPassword)).start(); } } |
Create A Folder and Copy All These Codes. Then To Run This Program, Follow The Following Steps To Run The Program.
Step 01:
Open Command Prompt and Go To File Directory In The Command Prompt. Compile All The Java Files, Create Remote Classes and Run The Registry Using Following Command.
javac *.java
rmic Server
rmic Client
rmiregistry
Step 02:
Open Another Command Prompt and Start The Server Using Following Command.
java Server
Step 03:
Open Another Command Prompt and Start A Client Using Following Command.
java Client
Step 04:
Open Another Command Prompt and Start Another Client Using Following Command.
java Client
Step 05:
Enter A Client Name and Password As Per The Server and Start Chatting.
Rather Than Running Thorough Typing Commands In The Command Prompt, Create Following Batch Files To Run It Easily.
1. Build.bat
javac *.java
rmic Server
rmic Client
rmiregistry
2. Start Server.bat
java Server
3. Start Client One
java Client
4. Start Client Two
java Client
Create Clients AS You Want and Start Chatting Using JAVA RMI CHAT PROGRAM.
1 comment:
how do i shut down client after logout? It seems a thread is not ended..
Post a Comment