簡體   English   中英

RMI(java.net.ConnectException:連接被拒絕:連接)

[英]RMI(java.net.ConnectException: Connection refused: connect)

我正在嘗試運行名為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

碼:

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是一個可以由rmiregistry &后台啟動的工具(JDK 7)。 我建議您檢查端口的防火牆或連接問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM