Java RMI服务器连接被拒绝

时间:2014-04-05 14:58:01

标签: java rmi

RMI界面:

import java.io.File;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;


public interface RMIinterface extends Remote 
{
    void getTrees() throws RemoteException;
}

服务器类:

import java.io.File;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

public class Server implements RMIinterface{ 

    List<File> server_file_tree, server_folder_tree;

    @Override
    public void getTrees() throws RemoteException {
        // TODO Auto-generated method stub
        server_file_tree = Shared.getFileTree(Shared.serverRoot);
        server_folder_tree = Shared.getFolderTree(Shared.serverRoot);
    }

    public static void main (String args[])
    {
        try
        {
            Server server = new Server();
            RMIinterface stub = (RMIinterface) UnicastRemoteObject.exportObject(server, 0);
            Registry reg = LocateRegistry.getRegistry();
            reg.bind("get_trees", stub);

            System.out.println("Server ready.");
        }catch (Exception e)
        {
            System.out.println("Server exception: " + e.toString());
            e.printStackTrace();
        }
    }





}

例外:

java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is: 
    java.net.ConnectException: Connection refused
java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is: 
    java.net.ConnectException: Connection refused
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:341)
    at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
    at Server.main(Server.java:27)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    ... 5 more

主机文件:

127.0.0.1   localhost
127.0.1.1   dereaper-G73Jh

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

为什么我会收到此异常,我该怎么办呢?

2 个答案:

答案 0 :(得分:1)

您还没有启动注册表。 LocateRegistry.getRegistry()没有做到这一点。见Javadoc。尝试createRegistry(),但请确保将结果存储在静态变量中。

答案 1 :(得分:-1)

在服务器类中:

System.setProperty("java.rmi.server.hostname","ipaddress");