简体   繁体   English

尝试连接到RMI注册表,但得到java.net.ConnectException:连接被拒绝:connect

[英]Trying to connect to RMI Registry but getting java.net.ConnectException: Connection refused: connect

I have two computers. 我有两台电脑。 On one of them, is running an RMI Registry - which was created from this code alone: 其中一个正在运行RMI注册表-仅通过以下代码创建:

package main;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

public class TheRegistry{

    public static void main(String[] args) {
        try {
            Registry reg = LocateRegistry.createRegistry(2020);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            System.out.println("Registry Created");
            Scanner input = new Scanner(System.in);
            input.nextInt();
            System.exit(0);
        }
    }
}

The other computer has a server that is trying to register an Object on this registry, however, it gets an exception. 另一台计算机上有一个服务器正在尝试在此注册表上注册一个对象,但是,它得到了一个例外。 Here is the code for the server: 这是服务器的代码:

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;

public class TextScramblerServer implements TextScramblerInterface 
{
    private static Remote obj; 
    // main method to export
    @Override //Return input text as-is.
    public String testInputText(String inputText) {

        return "Your input text is: " + inputText;
    }

    @Override //Return the string reversed.
    public String reverse(String inputText) {
        String reversedInput = "";
        for(int i=0; i<inputText.length();i++)
        {
            reversedInput=reversedInput+inputText.charAt((inputText.length()-1)-i);
        }
        return "Result: "+reversedInput;
    }

    @Override //Return the string scrambled.
    public String scramble(String inputText) {
        String scrambledInput="";

        for(int i=0; i<inputText.length();i++)
        {
            if(i%2==0)
            {
                scrambledInput=scrambledInput+inputText.charAt(i);
            }
            else
            {
                scrambledInput=inputText.charAt(i)+scrambledInput;
            }
        }
        return "Result: "+scrambledInput;
    }

    public void exportServer() throws Exception {
        System.setSecurityManager(new RMISecurityManager());
        obj = UnicastRemoteObject.exportObject(this, 2022);
        Registry registry = LocateRegistry.getRegistry("132.205.94.50", 2020);
        registry.bind("test", obj);
    }

    public static void main(String[] args) {
        try {
            (new TextScramblerServer()).exportServer();
            System.out.println("Server is up and running");
        }
        catch(Exception e){
            e.printStackTrace();
            try {
                UnicastRemoteObject.unexportObject(obj, true); //close port
            } catch (NoSuchObjectException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
}

I keep getting the error: 我不断收到错误:

java.rmi.ConnectException: Connection refused to host: 132.205.94.50; nested exception is: 
    java.net.ConnectException: Connection refused: connect
    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.bind(Unknown Source)
    at TextScramblerServer.exportServer(TextScramblerServer.java:57)
    at TextScramblerServer.main(TextScramblerServer.java:62)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    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
java.rmi.NoSuchObjectException: object not exported
    at sun.rmi.transport.ObjectTable.unexportObject(Unknown Source)
    at java.rmi.server.UnicastRemoteObject.unexportObject(Unknown Source)
    at TextScramblerServer.main(TextScramblerServer.java:68)

I can't figure out why this is happening. 我不知道为什么会这样。 I think I've tried everything 我想我已经尝试了一切

I ran your code and it worked for me after configuring the security policy. 我运行了您的代码,并在配置安全策略后为我工作

Your ConnectionRefused exception means that the underlying TCP connection cannot be established. ConnectionRefused异常意味着无法建立基础的TCP连接。 It's network issue, not an RMI issue. 这是网络问题,而不是RMI问题。

Try running both the server and registry on the same host, and use localhost as the hostname. 尝试在同一主机上同时运行服务器和注册表,并使用localhost作为主机名。 If it works, the problem is likely a firewall issue between the two hosts. 如果有效,则问题可能是两台主机之间的防火墙问题。

You can do a simple test of a TCP connection to the specific port using telnet. 您可以使用telnet对与特定端口的TCP连接进行简单测试。 If the port isn't listening, telnet will give you a similar connection refused message. 如果端口未在监听,则telnet会给您类似的拒绝连接消息。 If the port is listening, you'll get something like this on the terminal: 如果端口正在侦听,您将在终端上看到以下内容:

Connected to localhost.
Escape character is '^]'.

Control-C to get out of the session. Ctrl-C退出会话。

The specific telnet output may vary based on your OS, but they are all about the same. 特定的telnet输出可能会因您的操作系统而异,但是它们几乎相同。

If it is a firewall issue, you'll have to open up the ports. 如果是防火墙问题,则必须打开端口。 How to do that depends on OS, but it's easy to find. 具体操作取决于操作系统,但是很容易找到。

Either your Registry has been garbage-collected, you got the IP address wrong, or it is a public IP address and you haven't configured port forwarding. 您的注册表已被垃圾回收,您输入了错误的IP地址,或者它是公用IP地址,并且尚未配置端口转发。

You need to store the Registry reference in a static object to overcome garbage collection, although what the point of that program is when rmiregistry.exe already exists escapes me completely. 您需要将注册表引用存储在一个静态对象中,以克服垃圾收集的问题,尽管rmiregistry.exe已经存在时该程序的意义是完全让我无法理解。

You're barking up the wrong tree anyway. 无论如何,您正在吠错树。 You can only bind to an RMI Registry that is running in the local host. 您只能绑定到在本地主机中运行的RMI注册表。 There is therefore never any need to use a Registry hostname other than "localhost" when binding or unbinding. 因此,在绑定或解除绑定时,不需要使用除"localhost"之外的其他注册表主机名。

The reason you got the NoSuchObjectException is that you are trying to unexport the stub, which is referred to by obj , which is the result of UnicastRemoteObject.exportObject() , which returns the stub. 出现NoSuchObjectException的原因是您试图取消导出存根(由obj引用),这是UnicastRemoteObject.exportObject()的结果,该结果返回存根。 See the Javadoc. 请参阅Javadoc。 You need to save the result of new TextScramblerServer() and unexport that. 您需要保存new TextScramblerServer()的结果并取消导出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RMI(java.net.ConnectException:连接被拒绝:连接) - RMI(java.net.ConnectException: Connection refused: connect) 获取java.net.ConnectException:连接被拒绝:在Eclipse中连接 - Getting java.net.ConnectException: Connection refused: connect in eclipse RMI服务器连接拒绝托管:#############; 嵌套的异常是:java.net.ConnectException:连接被拒绝:connect - RMI Server Connection refused to host: ############ ; nested exception is: java.net.ConnectException: Connection refused: connect Java Mail错误:java.net.ConnectException:连接被拒绝:connect - Java Mail Error : java.net.ConnectException: Connection refused: connect Java套接字 - java.net.ConnectException:连接被拒绝:连接 - Java sockets - java.net.ConnectException: Connection refused: connect Java网络问题:java.net.ConnectException:连接被拒绝:连接 - Java Networking Problem: java.net.ConnectException: Connection refused: connect Java 中的 ssh 异常:java.net.ConnectException:连接被拒绝:连接 - ssh Exception in Java: java.net.ConnectException: Connection refused: connect java.net.ConnectException:连接被拒绝:connect java nio - java.net.ConnectException: Connection refused: connect java nio java.net.ConnectException:连接被拒绝:connect openshift - java.net.ConnectException: Connection refused: connect openshift java.net.ConnectException:拒绝连接:连接HTTPS连接 - java.net.ConnectException: Connection refused: connect for HTTPS connections
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM