RMI(java.net.ConnectException:连接被拒绝:连接)
我试图运行名为SampleServer的服务器。我使用Windows,这是我做过什么:RMI(java.net.ConnectException:连接被拒绝:连接)
在cmd中:
javaw rmiregistry 1099
cd C:\Users\Home\workspace\RMI\src
java -Djava.security.policy=policy SampleServer 1099
我得到以下错误:
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
我使用不同的端口号,例如4719尝试为rmiregistry,但我收到相同的错误。我确定我的防火墙已被禁用,但问题仍然存在。我确定该端口尚未被使用。我真的希望有人能帮助我。
图片我的桌面与项目的文件夹,Eclipse窗口和CMD打开的: http://s22.postimg.org/uq00qzslr/picyture.png
代码:
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;
}
样品:
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();
}
}
政策:
grant {
permission java.security.AllPermission;
};
javaw rmiregistry 1099
停在那儿。这已经是错误的。 'rmiregistry'是一个可执行文件,而不是可以用'java'或'javaw'执行的Java类的名称。只需使用'rmiregistry'。
当端口上没有服务运行时,您尝试连接时发生此错误。正如EJP所说,rmiregistry
是一个可以在后台(JDK 7)中由rmiregistry &
启动的工具。我会建议你检查你的防火墙或端口连接问题。
因为他没有正确启动注册表,所以没有必要假定任何其他失败机制。 – EJP
好的,我只是想帮助他。而已。 – Keerthivasan
你不会通过提出不相关的问题来帮助任何人。 – EJP
但后来我得到:尝试不被识别为内部或外部命令。我以为启动的命令是启动rmiregistry,或者如果这不起作用,它不在我的情况,javaw rmiregistry)。是否有需要进行此项工作或特定目录的特定下载? – user2644819
'try'*不是内部或外部命令。去掉它。这里没有什么说使用它。你想要的命令是'rmiregistry'。显然你没有执行你认为正在执行的内容。 – EJP