我正在构建我正在构建的Java RMI系统。我尝试连接时不断收到UnknownHostException。对于主机名,我已经尝试了我的IPv4地址,IPv6地址,“localhost”,以及其他一些内容。我也尝试输入随机单词以查看是否存在差异。没有;我得到了完全相同的错误。 这是一些代码:
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
答案 0 :(得分:1)
LocateRegistry.getRegistry
方法采用普通主机名,但您传递的是URL。有一个两个arg形式,它也带有一个主机名和一个端口号。
如果要使用URL来查找RMI服务,请使用java.rmi.Naming.lookup()
。
答案 1 :(得分: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
在哪里进入?
这没有多大意义。