Chat program using Java RMI


A Chat program using Remote Method Invocation ( RMI )

import java.rmi.*;
public interface Chat extends java.rmi.Remote
{

public String send()throws RemoteException;
public void receive(String str)throws RemoteException;

}

—–

import java.rmi.*;
import java.rmi.ServerError.*;
import java.io.*;
public class ChatImpl extends java.rmi.server.UnicastRemoteObject implements Chat
{
public ChatImpl() throws java.rmi.RemoteException
{
super();

}
public String send()throws java.rmi.RemoteException

{try
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();System.out.println(str);
return str;
}catch(Exception e)
{
String str1=”some error”;
return str1;
}

}
public void receive(String str)throws java.rmi.RemoteException

{
System.out.println(str);
}
}

————

import java.rmi.*;
import java.io.*;
public class ChatClient implements Runnable{
static Chat c;
public static void main(String args[])throws IOException
{System.out.println(“enter static main”);

try
{
c=(Chat)Naming.lookup(“rmi://localhost/Chat”);
System.out.println(“lookupdone”);
}catch(Exception e){System.out.println(e);}
new ChatClient();
}
public ChatClient()
{
run();
}
public void run()
{

try{

String str=send();
System.out.println(“String is “+str);
c.receive(str);
System.out.println(c.send());
run();
}catch(Exception e){}
}
public String send()throws java.rmi.RemoteException

{try
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();

return str;
}catch(Exception e)
{
String str1=”some error”;
return str1;
}

}
public void receive(String str)throws java.rmi.RemoteException

{
System.out.println(str);
send();
}
}

—————-

import java.rmi.*;
import java.io.*;
public class ChatServer1
{
public ChatServer1() {

try{
ChatImpl c=new ChatImpl();
Naming.rebind(“//localhost:4444/Chat”,c);
}catch(Exception e){}
}
public static void main(String args[])throws IOException
{
try
{

new ChatServer1();
}catch(Exception e){}
}

}

Advertisements

One thought on “Chat program using Java RMI

  1. Hello,

    I am writing an application in android which will uses RMI call to connect to the server. I tried but could not workout.I googled but didn’t found any solution.

    Could you please help me in this regard,

    Thanks in advance,
    Regards,
    Karthik P
    karthik.peddineni@gmail.com

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s