这是个普遍的rmi问题,关键是rmi默认的ip是内网那个ip,所以你得需要指定一下ip就可以了,比如你在启动你的rmi server的时候,需要在你的代码前设置一下,加入
System.setProperty("java.rmi.server.hostname","外网的ip");

解决方案 »

  1.   

    不要意识,添加那段程序还是有问题啊!!!
    兄台帮个忙吧!!!
    我在server端的程序如下
    import java.net.InetAddress;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;import javax.swing.JOptionPane;import com.teamax.xgis.rmi.XPubrmi.server.xgisXPubRMIConnectionServe;
    import com.teamax.xgis.rmi.XPubrmi.server.xgisXPubRMIConnectionServerSend;public class RMIMainApp
    {
    public static void main(String args[])
    {
    RMIMainApp app = new RMIMainApp();
    app.execute();
    // try
    // {
    // xgisRMIDisconnectionServerSend obj1 = new xgisRMIDisconnectionServerSend();
    // xgisRMIDisconnectionServer stub1 = (xgisRMIDisconnectionServer) UnicastRemoteObject.exportObject(obj1, 0);
    // String ip = InetAddress.getLocalHost().getHostAddress();
    // // Bind the remote object's stub in the registry
    // Registry registry = LocateRegistry.getRegistry(ip, 8846);
    // registry.bind("xgisClient1", stub1);
    //
    // xgisRMIConnectionServerSend obj2 = new xgisRMIConnectionServerSend();
    // xgisRMIConnectionServe stub2 = (xgisRMIConnectionServe) UnicastRemoteObject.exportObject(obj2, 0);
    // registry.bind("xgisClient2", stub2);
    //
    // System.out.println("Server ready");
    // }
    // catch (Exception e)
    // {
    // System.err.println("Server exception: " + e.toString());
    // e.printStackTrace();
    // }
    }

    public void execute()
    {
    try
    {
    String ip = InetAddress.getLocalHost().getHostAddress();
    Registry registry = LocateRegistry.getRegistry(ip, 8846);
    xgisXPubRMIConnectionServerSend obj2 = new xgisXPubRMIConnectionServerSend(ip, 8849);
    xgisXPubRMIConnectionServe stub2 = (xgisXPubRMIConnectionServe) UnicastRemoteObject.exportObject(obj2, 0);
    registry.bind("xgisClient2", stub2);

    System.out.println("Server ready");
    }
    catch (Exception e)
    {
    JOptionPane.showMessageDialog(null, "RMI for XPub has not been registryed!", "RMI For XPub", 1);
    System.exit(0);
    }
    }
    }//////////////////////////////////////////
    客户端方法体为:
    /**
     * 执行方法
     * 进行登录的方法
     */
    public void execute()
    {
    // Client cc = new Client(myApplication);
                    //获取公网IP
    String ip = myApplication.getCenterAddress();

    try
    {
    if(myApplication.getXPubRMI() == null)
    {
    stub2 = (xgisXPubRMIConnectionServe)Naming.lookup("//"+ip+":8846/xgisClient2");
    myApplication.setXPubRMI(stub2);
    myApplication.setXPubClientMode("online");
    }
    String info[] = login.getInfo();
    System.out.println("登录事件");
    xgisProperties xp = new xgisProperties();
    xp.setProperty(XPubCommandConstants.XPUB_LOGIN, info);
    xgisInformation xf = new xgisInformation( XPubCommandConstants.XPUB_LOGIN, xp); myApplication.sendToXPubAndExecuteResult(xf);
    }
    catch( Exception e)
    {
    e.printStackTrace();
    } }///////////////////////////////////////
    stub2 = (xgisXPubRMIConnectionServe)Naming.lookup("//"+ip+":8846/xgisClient2");
    会抛出如下错误:java.rmi.ConnectException: Connection refused to host: 61.29.147.250; 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 XPub.tools.operations.usermanage.LoginOp.execute(LoginOp.java:67)
    at teamax.tools.PlainTool.actionPerformed(PlainTool.java:108)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(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)
    ... 32 more
      

  2.   

    把我的那句话加在
    RMIMainApp app = new RMIMainApp();的前面
      

  3.   

    哦,不对,我没有仔细看,你的服务器公网ip是路由器映射过来的,是吧?
    如果真是这样,好像RMI是透不过来的,我当时作rmi那个项目的时候,几乎试过了所有的方法,终以失败告终,如果你的数据量不太大的话,我建议你改成web service方式