[英]connecting client-server RMI
如果服務器是遠程的(與客戶端不在同一台計算機上),我對客戶端實際上如何建立與服務器的連接感到困惑。 我的代碼使用localhost可以正常工作,但我無法弄清楚客戶端實際上是如何與服務器主機建立連接的,因此它可以查找rmiregistry。 我對服務器注冊表中存儲的內容感到困惑,是Sample還是localhost? 這可能是愚蠢的,但我嘗試將localhost轉換為客戶端的ipaddress並執行String url =“ //” +服務器+“:” + 1099 +“ / Sample”; 其中服務器是getbyname()的ip,但我得到一個例外:java.rmi.NotBoundException:127.0.0.1:1099/Sample這是兩台計算機上的客戶端和服務器。 我只是想弄清楚兩者如何遠程連接,但是使用本地主機的ip地址甚至無法在同一台機器上工作。
客戶:
import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class SampleClient {
public static void main(String args[]) {
String url = "//" + "localhost" + ":" + 1099 + "/Sample";
SampleInterface sample = (SampleInterface)Naming.lookup(url);
} catch(Exception e) {
System.out.println("SampleClient exception: " + e);
}
}
}
服務器:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class SampleServer {
public static void main(String args[]) throws IOException {
// Create and install a security manager
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
try {
String url = "//localhost:" + 1099 + "/Sample";
System.out.println("binding " + url);
Naming.rebind(url, new Sample());
// Naming.rebind("Sample", new Sample());
System.out.println("server " + url + " is running...");
}
catch (Exception e) {
System.out.println("Sample server failed:" + e.getMessage());
}
}
}
服務器應綁定到在“ localhost”上運行的注冊表。
客戶端應在服務器主機上查找注冊表。
就這么簡單。
我對服務器注冊表中存儲的內容感到困惑,是Sample還是localhost?
都不是。 您在混淆三件事:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.