문제

I have some problems to connect the remote object for client class. I get java.rmi.ConnectException when I run following client class.

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

The server side is following:

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

The exception when I run Client class is:

    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
도움이 되었습니까?

해결책

The issue was with the port number at client side. Specify the port number also while getting the Registry.

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

Returns a reference to the the remote object Registry for the local host on the default registry port of 1099.

Try

Client.java (specify the port no)

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

or

Server.java (use default port no)

Registry registry = LocateRegistry.createRegistry(1099);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top