2012-06-16 133 views
1

我已經創建了一個簡單的Java RMI程序來了解它是如何工作的。但是當我嘗試運行我的服務器端時,它引發了以下異常。如何在java程序中解決java.rmi.ConnectException?

編輯:我們正在使用代理服務器連接...

Remote exception: java.rmi.ConnectException: Connection refused to host: 10.7.150.18; nested exception is: 
    java.net.ConnectException: Connection refused: connect 

這是供你參考我的服務器端代碼......

import java.rmi.*; 
import java.rmi.server.*; 
import java.rmi.registry.*; 

public class SampleServerImpl extends UnicastRemoteObject implements SampleServer 
{ 
    SampleServerImpl() throws RemoteException 
    { 
    super(); 
    } 
    @Override 
    public int sum(int a,int b) throws RemoteException 
    { 
    return a + b; 
    } 

    public static void main(String[] args) 
    { 
     try 
     { 
     //set the security manager 
     //System.setSecurityManager(new RMISecurityManager()); 
     //create a local instance of the object 
     SampleServerImpl Server = new SampleServerImpl(); 
     //put the local instance in the registry 
     Naming.rebind("//10.7.150.18:9999" , Server); 
     System.out.println("Server waiting....."); 
     } 
     catch (java.net.MalformedURLException me) 
     { 
     System.out.println("Malformed URL: " + me.toString()); 
     } 
     catch (RemoteException re) 
     { 
     System.out.println("Remote exception: " + re.toString()); 
     }  
    } 
} 

請指引我走出這個問題...

+0

提供的信息不足。綁定時你得到這個嗎?仰視時?或者當執行遠程方法? – EJP

+0

@EJP:當使用java.exe運行這個文件時 – Saravanan

+0

這實際上並沒有回答我的問題,並且答案就在您應該發佈的堆棧跟蹤中,但是如果您的意思是綁定時發生,那就意味着RMI註冊表不在端口9999上的該主機上運行。 – EJP

回答

0

Naming類提供了用於存儲和獲取對遠程對象註冊表中遠程對象的引用的方法。 Naming類的每個方法都需要作爲名稱的形式爲在URL格式是java.lang.String(不含方案成分)它的一個參數:

//host:port/name 

與在URL格式添加一個名稱

//10.7.150.18:9999/Server 
+0

:我們在代理環境中使用RMI。這是否會導致任何問題? – Saravanan

+0

在代理環境中?完全無法理解這一點。你的服務器和客戶端可以互相訪問嗎?或者你的意思是說你使用一些代理服務器訪問你的服務器? –

+0

@實際上,我們的校園通過代理設置完全限制了互聯網訪問。所以,現在我正在使用客戶端和服務器機器在同一個園區。兩臺服務器機器和客戶端機器都在代理設置下工作。我現在該做什麼? – Saravanan