【问题标题】:RMI(java.net.ConnectException: Connection refused: connect)RMI(java.net.ConnectException:连接被拒绝:连接)
【发布时间】:2013-11-05 10:30:45
【问题描述】:

我正在尝试运行名为 SampleServer 的服务器。我正在使用 Windows,这就是我所做的:

在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

代码:

样本服务器:

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());
        }
    }
}

示例客户端:

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);
        }
    }
}

示例接口:

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;
};

【问题讨论】:

    标签: java networking permissions localhost rmi


    【解决方案1】:

    javaw rmiregistry 1099

    停在那里。这已经是错误的了。 'rmiregistry' 是一个可执行文件,而不是您可以使用 'java' 或 'javaw' 执行的 Java 类的名称。只需使用“rmiregistry”即可。

    【讨论】:

    • 但后来我得到:try 未被识别为内部或外部命令。我认为启动的命令是启动 rmiregistry,或者如果这不起作用,在我的情况下它不起作用,javaw rmiregistry)。是否有特定的下载需要我完成这项工作或特定的目录?
    • 'try' 不是内部或外部命令。去掉它。这里没有说要使用它。你想要的命令是'rmiregistry'。显然,您没有执行您认为正在执行的操作。
    【解决方案2】:

    当端口上没有运行服务时会发生此错误,您正在尝试连接。正如 EJP 所说,rmiregistry 是一个可以在后台由rmiregistry &amp; 启动的工具(JDK 7)。我建议您检查防火墙或端口的连接问题。

    【讨论】:

    • 由于他实际上并没有正确启动注册表,所以没有必要假设任何额外的失败机制。
    • 你提出无关紧要的事情不会帮助任何人。
    猜你喜欢
    • 2017-08-10
    • 2023-03-21
    • 2015-05-18
    • 2018-06-10
    • 2014-05-17
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多