2014-11-21 99 views
1

我們正試圖讓RMI通過Internet工作。我們試過的:通過互聯網的Java RMI。主機拒絕連接

  1. 客戶端和服務器端口的端口轉發(1099-1100)。
  2. 關閉Windows防火牆和路由器
  3. 與tunngle試過(www.tunngle.net/)

我們的RMI接口:

import java.rmi.RemoteException; 

public interface RMIInterface extends java.rmi.Remote { 
    public void helloWorld(int i) throws RemoteException; 
} 

我們的RMI服務器實施:

import java.net.MalformedURLException; 
import java.rmi.AlreadyBoundException; 
import java.rmi.Naming; 
import java.rmi.RemoteException; 
import java.rmi.registry.LocateRegistry; 
import java.rmi.registry.Registry; 
import java.rmi.server.UnicastRemoteObject; 

public class RMIServerTest extends UnicastRemoteObject implements RMIInterface { 

    public RMIServerTest() throws RemoteException { 
    } 

    @Override 
    public void helloWorld(int i) throws RemoteException { 
      for (int j = 0; j < i; j++) { 
        System.out.println("Hello World"); 
      } 
    } 

    public static void main(String[] args) { 
      try { 
        LocateRegistry.createRegistry(Registry.REGISTRY_PORT); 
      } 

      catch (RemoteException ex) { 
        System.out.println(ex.getMessage()); 
      } 
      try { 
        Naming.rebind("Server", new RMIServerTest()); 
      } catch (MalformedURLException ex) { 
        System.out.println(ex.getMessage()); 
      } catch (RemoteException ex) { 
        System.out.println(ex.getMessage()); 
      } 

    } 
} 

和我們的客戶:

import java.net.MalformedURLException; 
import java.rmi.Naming; 
import java.rmi.NotBoundException; 
import java.rmi.RemoteException; 
import java.rmi.registry.LocateRegistry; 


public class RMIClient { 
    public static void main(String[] args) throws RemoteException, NotBoundException,MalformedURLException { 
    try { 

     RMIInterface serverObject = (RMIInterface) Naming.lookup("//externalServerAdress/Server"); 
     serverObject.helloWorld(10); 
    } 
    catch (Exception e) { 
     System.out.println(e.getMessage()); 

    } 


    } 
} 

我們仍然收到此錯誤:

Connection refused to host: 192.168.0.13; nested exception is: 
java.net.ConnectException: Connection timed out: connect 

192.168.0.13是他的路由器後面的服務器的本地IP-ADRESS。我們在客戶端連接路由器的外部IP。比如「2.246.133.155」= externalServerAdress。 所以我們有一個連接。我們通過服務器的外部IP地址(WAN IP)連接並顯示錯誤,它獲取服務器的本地IP地址,但仍拒絕連接。

thx任何提示。

回答

0
Connection refused to host: 192.168.0.13 

這不是互聯網地址。這是一個私人地址,只存在於你的路由器後面。您需要使用您的公共 IP地址,並通過您的路由器安排端口轉發。

+0

我們在客戶端連接服務器網絡的路由器IP地址。我們真的不知道爲什麼我們在這裏得到他的路由器後面的服務器的本地IP地址。但它不是魔術,所以連接存在。 (我編輯了一篇重要的文章,使其更加清晰) – eikood 2014-11-21 23:12:10

+0

在這種情況下,您需要閱讀[RMI常見問題的項目A.1](https://docs.oracle.com/javase/6/docs/technotes/guides/rmi /faq.html#domain)。 – EJP 2014-11-21 23:14:50

+0

我們確實將外部WAN IP綁定在:System.setProperty(「java.rmi.server.hostname」,「xxx.xxx.xxx.xxx」);但總是得到相同的錯誤(連接拒絕主機:192.168.0.13 )它有點奇怪:/ – eikood 2014-11-22 10:37:11