2013-12-10 46 views
2

我遇到了我正在構建的Java RMI系統的問題。當我嘗試連接時,我不斷收到UnknownHostException。對於主機名稱,我嘗試了我的IPv4地址,IPv6地址,「本地主機」以及其他一些內容。我也試着隨機輸入文字,看看是否有區別。沒有;我得到了完全相同的錯誤。 下面是一些代碼:RMI UnknownHostException

public class GUI extends JFrame implements ActionListener, WindowListener 
{ 
public static GUI Global; 
private static final long serialVersionUID = 1L; 
public static void main(String[] args) 
{ 
    Global = new GUI(); 
    //Debugging 
    System.out.println(Global.Client.getServerMessage()); 
} 


public ClientIO() 
{ 
    System.setProperty("java.security.policy", "file:./bin/settings.policy"); 
    /*if (System.getSecurityManager() == null) 
    { 
     System.setSecurityManager(new RMISecurityManager()); 
    }*/  
    System.out.println("Enter the Server IP adress (The IP adress of the machine that the server is running on)"); 
    try 
    { 
     ClientIO.URL = new BufferedReader(new InputStreamReader(System.in)).readLine(); 
    } 
    catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    /*try { 
     URL = java.net.InetAddress.getLocalHost().getHostName(); 
    } catch (UnknownHostException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }*/ 
} 

public String getServerMessage() 
{ 
    String szRemoteAdress = URL+"/"+WebConstants.SERVICE_NAME; 
    try 
    { 
     Registry registry = LocateRegistry.getRegistry(szRemoteAdress); 
     EotGRemote stub = (EotGRemote) registry.lookup("EotGRemote"); 
     String response = stub.getTextyness(); 
     System.out.println("response: " + response); 
    } 
    catch (RemoteException | NotBoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return ""; 
} 

public class GlobalNetIO implements Runnable 
{ 
public static GlobalNetIO IO; 
ServerSocket m_MainServerSocket; 
ArrayList<Socket> m_Sockets; 
ArrayList<UserNetIO> m_UserInterfaces; 

public GlobalNetIO() 
{ 
    makeRMI(); 
    try 
    { 
     m_MainServerSocket = new ServerSocket(1111); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    //Define an ArrayList of the Socket 
    m_Sockets = new ArrayList<Socket>(); 
} 

public void makeRMI() 
{ 
    //I don't know what this means; I'm just going to accept it 
    System.setProperty("java.security.policy","file:./bin/settings.policy"); 
    //Might as well... 
    System.setProperty("java.rmi.server.codebase", "./"); 
    /*if (System.getSecurityManager() == null) 
     System.setSecurityManager (new RMISecurityManager());*/ 
    try 
    { 
     EotGRemote IO2 = new EotGRemoteIO(); 
     Registry registry = LocateRegistry.createRegistry(1099); 
     registry.bind(WebConstants.SERVICE_NAME, IO2); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

public interface EotGRemote extends Remote 
{ 
public String getTextyness() throws RemoteException; 
} 

package com.eotg.WebInterface; 

import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 
import com.eotg.WebUtils.*; 

public class EotGRemoteIO extends UnicastRemoteObject implements EotGRemote 
{ 
protected EotGRemoteIO() throws RemoteException 
{ 
} 

@Override 
public String getTextyness() { 
    // TODO Auto-generated method stub 
    return "It worked!"; 
} 
} 

而這裏的錯誤消息:

java.rmi.UnknownHostException: Unknown host: foo bar/Eo Nova; nested exception is: 
    java.net.UnknownHostException: foo bar/Eo Nova 
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source) 
    at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source) 
    at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source) 
    at sun.rmi.server.UnicastRef.newCall(Unknown Source) 
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) 
    at com.eotg.Client.ClientIO.getServerMessage(ClientIO.java:57) 
    at com.eotg.Client.GUI.main(GUI.java:34) 
Caused by: java.net.UnknownHostException: foo bar/Eo Nova 
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) 
    at java.net.PlainSocketImpl.connect(Unknown Source) 
    at java.net.SocksSocketImpl.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at java.net.Socket.<init>(Unknown Source) 
    at java.net.Socket.<init>(Unknown Source) 
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source) 
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source) 
    ... 7 more 
+2

請添加**確切**錯誤消息與呼叫跟蹤到您的文章。 –

回答

1

LocateRegistry.getRegistry方法接受一個普通的主機名,但你傳遞給它的URL。還有一個兩個參數的表單,它也需要一個主機名和一個端口號。

如果要使用URL來定位RMI服務,請使用java.rmi.Naming.lookup()

+0

我沒有使用URL;這只是變量的名稱。 – JDeveloper

0

「foo bar/Eo Nova」不是有效的主機名。

String szRemoteAdress = URL+"/"+WebConstants.SERVICE_NAME; 

在這裏,您將追加"/"+WebConstants.SERVICE_NAME到主機名。

Registry registry = LocateRegistry.getRegistry(szRemoteAdress); 

這裏您指定的不是主機名,而是您剛剛構建的組合字符串。只需在這裏使用主機名。

EotGRemote stub = (EotGRemote) registry.lookup("EotGRemote"); 

這裏您正在查找「EotGRemote」。 WebConstants.SERVICE_NAME在哪裏進入?

這沒有多大意義。

+0

我知道。但是無論我在鍵盤上輸入什麼,錯誤都是一樣的,所以我只用'foo bar'作爲例子。我可以輸入我的IP地址,'localhost',我的機器名稱,無論如何。我仍然得到同樣的錯誤。 – JDeveloper

+0

使用一個對別人有意義的例子會更重要,你不覺得嗎?問題是「/ Eo Nova」部分。 – EJP

+0

哦。非常抱歉! WebContants.SERVICE_NAME是一個只讀的字符串,其內容是「Eo Nova」。應該澄清一點。 – JDeveloper