gpt4 book ai didi

java - Raspberry Pi 上的 RMI 不起作用连接拒绝主机 : 127. 0.0.1

转载 作者:行者123 更新时间:2023-12-01 11:35:50 24 4
gpt4 key购买 nike

我正在尝试使用 java RMI 在服务器(在 Pi 上)和客户端(在另一个 PI 上)之间进行通信。客户端能够调用服务器的远程方法,但是当服务器尝试访问客户端的远程方法时,我收到错误 连接拒绝主机 127.0.0.1。 为了解决这个问题,我设置了 System.setProperty("java.rmi.server.hostname", ServerIP; 但随后出现 Connectionrejected to Host 10.10.*.*.

的错误

MasterInterface 看起来像

import java.rmi.Remote;
import java.rmi.RemoteException;


public interface MasterInterface extends Remote{
public void sayHello(String s) throws RemoteException;

}

Master.java 文件看起来像

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;


public class Master extends UnicastRemoteObject implements MasterInterface{

protected Master() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.setProperty("java.rmi.server.hostname", "192.168.4.*");
Master master = new Master();
Registry reg = LocateRegistry.createRegistry(9898);
reg.rebind("Master", master);
System.out.println("Master is on.123123132.test.");


} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

@Override
public void sayHello(String s) throws RemoteException {
// TODO Auto-generated method stub
System.out.println("Hiiiiiii ->>>>>"+s);
try{

Registry regi2 = LocateRegistry.getRegistry("192.168.*.*", 9898);
System.out.println("asdasdasd1!!");
SlaveInterface bsObj1 = (SlaveInterface) regi2.lookup("Slave");
bsObj1.saybye("madhav");
}catch(Exception e){
e.printStackTrace();
}

}

}

SlaveInterface 看起来像

    import java.rmi.Remote;
import java.rmi.RemoteException;


public interface SlaveInterface extends Remote{

public void saybye(String s) throws RemoteException;

}

Slave.java 文件看起来像

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;


public class Slave extends UnicastRemoteObject implements SlaveInterface {
private final static String master_IP = "192.168.4.*";
protected Slave() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
System.setProperty("java.rmi.server.hostname", "192.168.*.*");
Slave s = new Slave();
Registry regclient = LocateRegistry.createRegistry(2525);
System.out.println("slave obj bound!!");
regclient.rebind("Slave", s);
System.out.println("Slave is on..");

Registry regi = LocateRegistry.getRegistry(master_IP, 9898);
System.out.println("slave obj bound1111!!");
MasterInterface bsObj = (MasterInterface) regi.lookup("Master");
System.out.println("slave obj bound2222!!");
bsObj.sayHello("hi karan");
System.out.println("done................");
}catch(Exception e){
System.out.println("not working!!!!!!!!!!");
e.printStackTrace();
}

}

@Override
public void saybye(String s) throws RemoteException {
// TODO Auto-generated method stub
System.out.println("bye "+s);


}

}

最佳答案

在导出任何远程对象之前,将 java.rmi.server.hostname 设置为服务器 JVM 中服务器的外部 IP 地址。请参阅RMI FAQ item A.1 .

关于java - Raspberry Pi 上的 RMI 不起作用连接拒绝主机 : 127. 0.0.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30007713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com