RMI客户端java.rmi.ConnectException

时间:2014-03-15 01:03:12

标签: java exception connection client rmi

我在连接客户端类的远程对象时遇到了一些问题。当我运行以下客户端类时,我得到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

1 个答案:

答案 0 :(得分:1)

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

LocateRegistry.getRegistry("localhost")的Java文档说:

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

尝试

Client.java(指定端口号)

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

Server.java(使用默认端口号)

Registry registry = LocateRegistry.createRegistry(1099);