Thursday, 6 December 2012

RMI chat program

Chat program using java RMI

RMI Interface program

myInterface.java
import java.rmi.*;
public interface myInterface extends Remote
{
    public String message(String m)throws RemoteException;
}

Server program

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

public class myServer extends UnicastRemoteObject implements myInterface

{
    public myServer()throws RemoteException
    {
        super();
    }
   
    public String message(String m)throws RemoteException
    {
        return(m);
    }
   
    public static void main(String args[])
    {
        try{
        myServer ms=new myServer();
        Naming.rebind("rmi://hsj./myInterface",ms);/* your computer name must replace with the bold part*/
        System.out.println("Start..");
        }
        catch(Exception ex)
        {
        }
    }

}

RMI Client Program



import java.rmi.*;
import java.io.*;

public class myClient
{   
    public static String s,s1;
    public static void main(String args[])throws IOException,RemoteException
    {    try{
        myInterface mi=(myInterface)Naming.lookup("rmi://hsj./myInterface");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Your Messages, type 'Quit' to stop. ");
       
        s=br.readLine();
        s1=mi.message(s);
        System.out.println("<<" + s1);
       
        while(true)
        {
            System.out.println(">>");
            s=br.readLine();
            s1=mi.message(s);
            System.out.println("<<" + s1);
            if(s.equals("Quit"))
                System.exit(0);
        }
        }
        catch(Exception ex)
        {
        }

    }
}

output 

RMI chat program client
RMI Client

Server RMI output
RMI Server

 

No comments:

Post a Comment