RMI(java.net.ConnectException:连接被拒绝:连接)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19604517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RMI(java.net.ConnectException: Connection refused: connect)
提问by user2644819
I am trying to run my server named SampleServer. I am using windows and this is what i did:
我正在尝试运行名为 SampleServer 的服务器。我正在使用 Windows,这就是我所做的:
in cmd:
在 cmd 中:
javaw rmiregistry 1099
cd C:\Users\Home\workspace\RMI\src
java -Djava.security.policy=policy SampleServer 1099
i get the following error:
我收到以下错误:
binding //localhost:1099/Sample
New instance of Sample created
Sample server failed:Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
I've tried using a different port # such as 4719 for rmiregistry but i receive the same error. I made sure that my firewall was disabled but the problem persist. I made sure that the port is not already being used. I really hope someone can help me.
我尝试使用不同的端口号,例如 4719 用于 rmiregistry,但我收到相同的错误。我确保我的防火墙已禁用,但问题仍然存在。我确保该端口尚未被使用。我真的希望有人能帮助我。
Picture of my desktop with folders of project, eclipse window and cmd open: http://s22.postimg.org/uq00qzslr/picyture.png
我的桌面图片,其中包含项目文件夹、eclipse 窗口和 cmd 打开:http: //s22.postimg.org/uq00qzslr/picyture.png
Code:
代码:
SampleServer:
示例服务器:
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class SampleServer {
public static void main(String args[]) {
if (args.length != 1) {
System.err.println("usage: java SampleServer rmi_port");
System.exit(1);
}
// Create and install a security manager
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
try {
// first command-line argument is the port of the rmiregistry
int port = Integer.parseInt(args[0]);
String url = "//localhost:" + port + "/Sample";
System.out.println("binding " + url);
Naming.rebind(url, new Sample());
// Naming.rebind("Sample", new Sample());
System.out.println("server " + url + " is running...");
}
catch (Exception e) {
System.out.println("Sample server failed:" + e.getMessage());
}
}
}
SampleClient:
示例客户端:
import java.rmi.Naming;
import java.rmi.RemoteException;
public class SampleClient {
public static void main(String args[]) {
try {
if (args.length < 3) {
System.err.println("usage: java SampleClient host port string... \n");
System.exit(1);
}
int port = Integer.parseInt(args[1]);
String url = "//" + args[0] + ":" + port + "/Sample";
System.out.println("looking up " + url);
SampleInterface sample = (SampleInterface)Naming.lookup(url);
// args[2] onward are the strings we want to reverse
for (int i=2; i < args.length; ++i)
// call the remote method and print the return
System.out.println(sample.invert(args[i]));
} catch(Exception e) {
System.out.println("SampleClient exception: " + e);
}
}
}
SampleInterface:
示例界面:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface SampleInterface extends Remote {
public String invert(String msg) throws RemoteException;
}
Sample:
样本:
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.*;
// this is the class with remote methods
public class Sample
extends UnicastRemoteObject
implements SampleInterface {
private int a;
public Sample() throws RemoteException {
System.out.println("New instance of Sample created");
a = 1;
}
public String invert(String m) throws RemoteException {
// return input message with characters reversed
System.out.println("invert("+m+") a=" + a);
return new StringBuffer(m).reverse().toString();
}
}
policy:
政策:
grant {
permission java.security.AllPermission;
};
回答by user207421
javaw rmiregistry 1099
javaw rmiregistry 1099
Stop right there. This is already wrong. 'rmiregistry' is an executable, not the name of a Java class you can execute with 'java' or 'javaw'. Just use 'rmiregistry'.
停在那儿。这已经是错误的了。'rmiregistry' 是一个可执行文件,而不是您可以使用 'java' 或 'javaw' 执行的 Java 类的名称。只需使用'rmiregistry'。
回答by Keerthivasan
This error occurs when there is no service running on the port, you are trying to connect. As said by EJP, rmiregistry
is a tool which can be started by rmiregistry &
in the background (JDK 7). I would recommend you that you check your firewall or connectivity issue with the port.
当端口上没有运行任何服务,您正在尝试连接时,会发生此错误。正如 EJP 所说,rmiregistry
是一个可以rmiregistry &
在后台启动的工具(JDK 7)。我建议您检查端口的防火墙或连接问题。