我正在构建的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发布于 2013-12-11 06:24:45
LocateRegistry.getRegistry方法接受一个普通的主机名,但您向它传递的是一个URL。有一个两个参数的形式,它接受一个主机名和一个端口号。
如果您希望使用URL来定位RMI服务,请使用java.rmi.Naming.lookup()。
发布于 2013-12-11 06:30:05
"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在其中扮演了什么角色?
这没有多大意义。
https://stackoverflow.com/questions/20504898
复制相似问题