RMI or Remote Method Invocation is Java technology for implementing remote procedure calls. A RMI application has three parts. An interface that will extend java,rmi.Remote. This has to be shared by both the RMI Server & the RMI Client. An implementation class on the Server that shall implement the above mentioned interface.The implementation class must extend java.rmi.server.UnicastRemoteObject(for stateless calls). A server that shall create and bind a Object of the implementation to the RMI Registry. A RMI Client shall lookup the RMI Registry and create a proxy of the implementation Object and call the methods. Marshalling & unmarshalling of the parameters and return values is done through the stub classes. These again have to be present both on the client and the server.
The desktop capture is done through the use of the java.awt.Robot. All remotely callable methods must throw the java.rmi.RemoteException.
//*********************************************************************************
The Desktop Server
The interface IDesktop.java
import java.rmi.*;
public interface IDesktop extends Remote
{
public byte[] getDesktop() throws RemoteException;
}
The implementation Desktop.java
import java.rmi.*;
import java.rmi.server.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class Desktop extends UnicastRemoteObject implements IDesktop
{
public Desktop() throws RemoteException
{
}
public byte[] getDesktop()
{
try
{
GraphicsEnvironment env=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen=env.getDefaultScreenDevice();
Robot robot=new Robot(screen);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage img=robot.createScreenCapture(new Rectangle(0,0,d.width,d.height));
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", bytes );
bytes.flush();
byte[] data = bytes.toByteArray();
bytes.close();
return data;
}
catch(Exception ex)
{
System.out.println(ex);
return null;
}
}
}
The Server DesktopServer.java
import javax.naming.*;
import java.rmi.*;
public class DesktopServer
{
public static void main(String[] args)throws RemoteException,NamingException
{
Context cnt=new InitialContext();
cnt.bind("rmi:desktop",new Desktop());
System.out.println("Server is Ready");
}
}
Compile the classes and create the Stub Classes. Here is How .
Start the RMI Registry
The start the Server
//*********************************************************************************
The Desktop Client
Copy the IDesktop.class, and the Desktop_Stub.class files to the client.
DesktopClient.java
import java.rmi.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.naming.*;
import javax.swing.*;
import javax.imageio.*;
public class DesktopClient extends JFrame
{
//*************************************************************
class ImageThreader extends Thread
{
public void run()
{
while(true)
{
try
{
byte[] data = obj.getDesktop();
InputStream in = new ByteArrayInputStream(data);
BufferedImage img = ImageIO.read(in);
setIcon(img);
Thread.sleep(10000);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
}
//**************************************************************
private JLabel lbl;
private IDesktop obj;
public DesktopClient()throws RemoteException,NamingException,IOException
{
lbl=new JLabel();
add(lbl);
InitialContext cnt=new InitialContext();
obj=(IDesktop)cnt.lookup("rmi://compaq510/desktop");
//replace compaq510 with the name or IP Address of the computer running the server.
ImageThreader th=new ImageThreader();
th.start();
}
public void setIcon(BufferedImage img)
{
lbl.setIcon(new ImageIcon(img));
}
public static void main(String[] args)throws RemoteException,NamingException,IOException
{
DesktopClient f=new DesktopClient();
f.setVisible(true);
}
}
how to dispose the previous window in this application because after running this app there will be infinitely many screen shared because of polling after each 1000 ms
ReplyDeleteHello, champak you are great.. I mean you have innuf knowledge in RMI. but according your program we not get environment like Team Viwer. can you help me how we can implement this??
ReplyDeletegreat
ReplyDeletehello, iam looking for Screen Broadcasting to other PCs via RMI in java. anybody can help?
ReplyDeletethank you