gpt4 book ai didi

java - RMI 未知主机异常

转载 作者:行者123 更新时间:2023-11-30 11:24:56 25 4
gpt4 key购买 nike

我在构建 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

最佳答案

LocateRegistry.getRegistry 方法采用普通主机名,但您向它传递了一个 URL。有一个包含主机名和端口号的双参数形式。

如果您想使用 URL 来定位 RMI 服务,请使用 java.rmi.Naming.lookup()

关于java - RMI 未知主机异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20504898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com