运行RMI客户端错误消息

时间:2014-01-10 03:16:29

标签: java rmi rmiregistry

我修正了我们在课堂上所做的练习,所以它应该可以正常工作。但我无法正确执行它。

当我运行远程对象时它很好我在屏幕上显示消息显示它正常工作但是当我运行客户端后我发现了这个错误:

**java.rmi.ConnectException: Connection refused to host: Sony-PC; 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 java.rmi.Naming.lookup(Unknown Source)
    at fr.unice.miage.m1.distributedsystems.tp3.client.Client.main(Client.java:16)
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**
我绝望了,我尝试了一切。也许有人可以一步一步地告诉我如何让它发挥作用。谢谢你的帮助!

这是远程接口:

    import java.rmi.Remote;
    import java.rmi.RemoteException;

    public interface IRemote extends Remote {

    void echo() throws RemoteException;

    void echo(int value) throws RemoteException;

    String getRemoteAddress() throws RemoteException;

}

远程对象CLASS:

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.AlreadyBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class RemoteObject extends UnicastRemoteObject implements IRemote {

private static final long serialVersionUID = 1L;

protected RemoteObject() throws RemoteException {
    super();
}

@Override
public void echo() throws RemoteException {
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("RemoteObject.echo()");
}

@Override
public void echo(int value) throws RemoteException {
    System.out.println("RemoteObject.echo() value=" + value);
}

@Override
public String getRemoteAddress() throws RemoteException {
    try {
        return InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        return "Unknown";
    }
}

public static void main(String[] args) throws RemoteException,
        MalformedURLException, AlreadyBoundException {
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new RMISecurityManager());
    }

    int rmiRegistryPort = 1099;

    // the rmiregistry can either be created programmatically or started
    // manually before each run if you have some time to waste
    Registry registry = LocateRegistry.createRegistry(rmiRegistryPort);
    System.out.println("RMI registry listening on port " + rmiRegistryPort);

    RemoteObject remoteObject = new RemoteObject();
    registry.bind("myremoteobject", remoteObject);
}

}

客户端类

 import java.net.MalformedURLException;
    import java.net.UnknownHostException;
    import java.rmi.Naming;
    import java.rmi.NotBoundException;
    import java.rmi.RemoteException;

    import fr.unice.miage.m1.distributedsystems.tp3.server.IRemote;

    public class Client {

    public static void main(String[] args) {
        try {
            IRemote distante =
                    (IRemote)     Naming.lookup("rmi://"+java.net.InetAddress.getLocalHost()+"/myremoteobject");
            System.out.println("Client.main() before echo remote call");
            distante.echo();
            System.out.println("Client.main() after echo remote call");

            distante.echo(7);
            System.out.println("Remote object IP: "
                    + distante.getRemoteAddress());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

    }

0 个答案:

没有答案