【发布时间】:2020-11-22 06:51:18
【问题描述】:
我正在使用 Java RMI 编写应用程序。我已经创建了客户端和服务器。但是,我只使用本地主机。我希望能够使用域名让客户端联系服务器。我已经注册了一个域名:hotel.ddns.net。我希望能够将它保存在个人网络中,与端口转发无关。客户端和服务器位于不同的计算机上。我一直在寻找解决方案,但这些与我的问题没有直接关系。由于我还是新手,你们能帮帮我吗?
这是我的客户端代码:
package Hotel_;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java_rmi.java_rmi_interface;
public class ClientRMI {
public java_rmi_interface connectToServer() throws NotBoundException{
try{
Registry myregistry = LocateRegistry.getRegistry("127.0.0.1",1099);
java_rmi_interface myinterface = (java_rmi_interface)myregistry.lookup("bookingServer");
System.out.println("client reaaaaaaady");
return myinterface;
}catch(RemoteException e){
e.printStackTrace();
}
return null;
}
}
这是我的服务器代码
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class ServerRMI {
public static void main(String[] args){
try{
//Timer timer = new Timer();
//timer.deletefunc();
Registry myregitry = LocateRegistry.createRegistry(1099);//create a registry that can be
accessed on this port
QueryCentre qs = new QueryCentre();//an instance of QueryCenter where remote methods are
found
Naming.rebind("bookingServer", qs);//naming the server and passing the class where remote
methods are found
System.out.println("server ready and waitiiiinnnggg!!!!!");
}catch(RemoteException | MalformedURLException e){
e.printStackTrace();
}
}
}
【问题讨论】:
-
这没有意义。如果您在本地网络中工作并且不需要将其公开到 Internet,那么您必须在网络中使用 IP 地址或提供一些名称解析方式(即 DNS 服务器)。动态 DNS 在 Internet 上工作,而不是在您的 NATted LAN 中使用私有地址。