帮助解决java rmi任务中的问题 [英] help for solving problems in a java rmi assignment

查看:169
本文介绍了帮助解决java rmi任务中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个应用程序,用java rmi在客户端和服务器之间共享桌面。这是界面:

I want to write an application to share desktop between client and server with java rmi. This is the interface:

public interface IServer extends Remote{

    public void share(BufferedImage image) throws RemoteException;
}
                  -----------------------------

//这是服务器端代码:

// This is the server side code:

public class ServerFrame extends JFrame implements IServer{
public static void main(String args[]) {
        try {

             ServerFrame frame = new ServerFrame();

             frame.setVisible(true);
             LocateRegistry.createRegistry(1099);
             Naming.bind("test", frame);
             System.out.println("Server Started Successfully...");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
. . . 
public ServerFrame() {

        super();
          try {
            UnicastRemoteObject.exportObject((Remote) this);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
  . . .  // bulding the ServerFrame GUI
. . .   
public void share(BufferedImage image) throws RemoteException {
        label.setIcon(new ImageIcon(image));
    }
}
                  ---------------

//最后这是客户端:ClientFrame中有一个按钮,我希望在单击此按钮后在客户端和服务器之间共享桌面。

// And finally this is the client side: there is a button in the ClientFrame which I want the desktop to be shared between client and server after I clicked this button.

public class ClientFrame extends JFrame implements Remote,Serializable{

    IServer serve;
...
public ClientFrame() {
        super();
         try {
            serve = (IServer) Naming.lookup("test");
        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (RemoteException e) {

            e.printStackTrace();
        } catch (NotBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        . . . // codes for building client gui
    final JButton shareDesktopButton = new JButton();
        shareDesktopButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent arg0) {
                try {
                    BufferedImage screenShot = new Robot()
                    .createScreenCapture(new Rectangle(Toolkit
                            .getDefaultToolkit().getScreenSize()));
                    label.setIcon(new ImageIcon(screenShot));
                    serve.share(screenShot);
                } catch (HeadlessException e) {

                    e.printStackTrace();
                } catch (AWTException e) {

                    e.printStackTrace();
                } catch (RemoteException e) {

                    e.printStackTrace();
                }

            }
        });
          . . .
     }
// but there is a problem that causes these exceptions that I can not understand it. Please help me to complete my project.

非常感谢

异常:

java.rmi.MarshalException: error marshalling arguments; nested exception is: 
    java.io.NotSerializableException: java.awt.image.BufferedImage
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at server.ServerFrame_Stub.share(Unknown Source)
    at client.ClientFrame$1.mouseClicked(ClientFrame.java:86)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.io.NotSerializableException: java.awt.image.BufferedImage
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at sun.rmi.server.UnicastRef.marshalValue(Unknown Source)
    ... 24 more


推荐答案

你不能使用 BufferedImage 作为远程参数,因为它既不是远程也不是 Serializable

You can't use BufferedImage as a remote parameter as it is neither Remote nor Serializable.

您可以在 ImageIcon 中包装 BufferedImage ,但这是非常低效,因为它将被转换为位图并通过网络传输未压缩。

You could wrap the BufferedImage in an ImageIcon but that is very inefficient as it will be converted to a bitmap and transmitted over the network uncompressed.

我会将参数设为分享表示压缩图像格式的字节数组(例如, PNG。)

I would make the argument to Share a byte array representing a compressed image format (e.g. PNG.)

public interface IServer extends Remote{
   public void share(byte[] imagePNGBytes) throws RemoteException;
}

public void mouseClicked(MouseEvent arg0) {
    try {
        BufferedImage screenShot = new Robot()
        .createScreenCapture(new Rectangle(Toolkit
        .getDefaultToolkit().getScreenSize()));
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        ImageIO.write(screenShot, "PNG", bytes);
        server.share(bytes.toByteArray());
    } catch (...) {
         // ...
    }
}

public class ServerFrame extends JFrame implements IServer {

    // . . .

    public void share(byte[] imagePNGBytes) throws IOException, RemoteException {
        RenderedImage image = ImageIO.read(new ByteArrayInputStream(imagePNGBytes));
        label.setIcon(new ImageIcon(image));
    }
}

这篇关于帮助解决java rmi任务中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆