RMI Client java.rmi.ConnectException



我有一些问题连接远程对象的客户端类。当我运行以下客户端类时,我得到java.rmi.ConnectException。

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
    /**
     * Declare Payment Stub object.
     */
    private static Payment stub = null;
    private Client() {
    }
    public static void main(String[] args) {
        double principal = 80000;
        double annualInterest = .065;
        int years = 15;
        /**
         * Try to connect the server and look up the
         * defined stub.
         */
        try {
            Registry reg = LocateRegistry.getRegistry("localhost");
            stub = (Payment) reg.lookup("Mortgage");
        } catch (Exception e) {
            System.err.println("Client exception thrown: " + e.toString());
            e.printStackTrace();
        }               
    }

服务器端如下:

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
public class Server {
    public Server() {
    }
    public static void main(String args[]) {
        try {
            PaymentImpl robj = new PaymentImpl();
            Payment stub = (Payment) UnicastRemoteObject.exportObject(robj, 9260);
            Registry registry = LocateRegistry.createRegistry(9260);
            registry.bind("Mortgage", stub);
            System.out.println("Mortgage Server is ready to listen... ");
        } catch (Exception e) {
            System.err.println("Server exception thrown: " + e.toString());
            e.printStackTrace();
        }
    }
}

当我运行Client类的异常是:

    Client exception thrown: java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect
java.rmi.ConnectException: Connection refused to host: localhost; 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.lookup(Unknown Source)
    at Client.main(Client.java:24)
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)
    ... 6 more

问题是客户端端口号。在获取Registry时也指定端口号。

Java Doc for LocateRegistry.getRegistry("localhost")说:

返回对本地主机在默认注册表端口1099上的远程对象注册表的引用。

Client.java(指定端口号)

Registry reg = LocateRegistry.getRegistry("localhost",9260);

Server.java(使用默认端口号)
Registry registry = LocateRegistry.createRegistry(1099);

最新更新