【问题标题】:RMI client 'cannot cast' exceptionRMI 客户端“无法转换”异常
【发布时间】:2018-08-25 15:26:20
【问题描述】:

我正在尝试在 RMI 中编写一个从服务器程序复制文件的客户端程序:

FileClientInt 接口

public interface FileClientInt extends Remote {

    public boolean sendData(String fileName, byte[] data, int len) throws RemoteException;

    public String getName() throws RemoteException;

}

FileServerInt 接口

public interface FileServerInt extends Remote {


public boolean login(FileClientInt c) throws RemoteException;

}

文件客户端类

public class FileClient extends UnicastRemoteObject implements FileClientInt {

/**
 *
 */
private static final long serialVersionUID = 1L;
public String name;

public FileClient(String n) throws RemoteException {
    super();
    name = n;
}

public String getName() throws RemoteException {
    return name;
}

public boolean sendData(String filename, byte[] data, int len) throws RemoteException {
    try {
        File f = new File(filename);
        f.createNewFile();
        FileOutputStream out = new FileOutputStream(f, true);
        out.write(data, 0, len);
        out.flush();
        out.close();
        System.out.println("Done writing data...");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}

StartFileClient 类

public class StartFileClient {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
        FileClient c = new FileClient("out.txt");
        FileServerInt server  = (FileServerInt) Naming.lookup("rmi://192.168.2.15");            
        server.login(c);
        System.out.println("Listening.....");
        Scanner s = new Scanner(System.in);
        while (true) {
            String line = s.nextLine();
        }
    } catch (Exception e) {
        System.out.println("Error " + e.getMessage());
    }
}

它给了我错误:

Error sun.rmi.registry.RegistryImpl_Stub cannot be cast to myfileclient.FileServerInt

我该如何解决这个错误?任何机构都可以帮助解决这个问题吗?

【问题讨论】:

    标签: java registry rmi classcastexception stub


    【解决方案1】:

    您忘记将对象名称放在注册表查找字符串的末尾。所以你得到了注册表本身,而不是你正在查找的远程接口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2013-12-09
      相关资源
      最近更新 更多