1

首先是我得到的例外:http: //i.imgur.com/dE5Ou.png

只是为了提供一点背景知识,我正在尝试编写简单的 RMI 程序,该程序使用 java 的 RMI 连接两台远程计算机(客户端/服务器)。我已经启动并运行了我的服务器程序,但是当我运行我的客户端程序时,我得到了上面链接中显示的异常。既然我告诉它连接到 192.168.0.104,为什么它说“连接拒绝托管:127.0.1.1”???

客户

public class Client 
{
public static void main(String[] args)
{
ServerInterface server;     
Registry registry;

try
    {
    registry = LocateRegistry.getRegistry("192.168.0.104", (new Integer(1099)).intValue());         

    server = (ServerInterface)Naming.lookup("//192.168.0.104/ServerTest");

    String serverString = server.getAndSetMessage("Connecting");

    System.out.println("Reply from the server is: " + serverString);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    System.exit(1);
    }


}
}

服务器

public class Server extends UnicastRemoteObject implements ServerInterface 
{

static String hostName = "192.168.0.104";
String name;

public Server(String name) throws RemoteException
{
    super();
    this.name = name;

}

public String getAndSetMessage(String message) throws RemoteException
{
    return("My name is " + name + " Thanks for message " + message);
}

public static void main(String args[]) 
{
    try
    {
        String objectname = "ServerTest";

        Server theServer = new Server(objectname);
        Naming.rebind("//"+hostName+"/"+objectname,theServer);

        System.out.println("//"+hostName+"/"+objectname);
        System.out.println("I am Registered");

    }
    catch (Exception ex)
    {
        System.out.println(ex);
        System.exit(1);
    }

}

}
4

1 回答 1

2

您可以尝试将以下代码添加到服务器:

System.setProperty("java.rmi.server.hostname", "192.168.0.104");
于 2012-10-29T18:56:53.030 回答