当前页面: 开发资料首页 → J2EE 专题 → RMI远程连接问题(公网)
RMI远程连接问题(公网)
摘要: RMI远程连接问题(公网)
服务器是双网卡,ip分别是:192.168.0.200(对外网) 10.33.20.49(对内网) ,然后对外公网静态ip是61.29.147.250,这个ip是设置在路由器上面的.现在公司内网几台电脑都连在集线器上面对应的是服务器的192.168.0.200(对外网)这张网卡,我们这几台电脑都不能上网.用RMI连接服务器在Naming.lookup("//61.29.147.250: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.RegistryUserOp.run(RegistryUserOp.java:81)
at java.lang.Thread.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.
(Unknown Source)
at java.net.Socket.(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 8 more
这是个普遍的rmi问题,关键是rmi默认的ip是内网那个ip,所以你得需要指定一下ip就可以了,比如你在启动你的rmi server的时候,需要在你的代码前设置一下,加入
System.setProperty("java.rmi.server.hostname","外网的ip");
不要意识,添加那段程序还是有问题啊!!!
兄台帮个忙吧!!!
我在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.(Unknown Source)
at java.net.Socket.(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 32 more
把我的那句话加在
RMIMainApp app = new RMIMainApp();的前面
哦,不对,我没有仔细看,你的服务器公网ip是路由器映射过来的,是吧?
如果真是这样,好像RMI是透不过来的,我当时作rmi那个项目的时候,几乎试过了所有的方法,终以失败告终,如果你的数据量不太大的话,我建议你改成web service方式
终于搞定,原来是路由器没有设置好.只要把端口映射到服务器就可以了.谢谢各位!