LAN网络上的远程方法调用

时间:2017-02-06 16:21:19

标签: java networking rmi lan

美好的一天。

我试图让2个(非常基本的)java程序使用RMI进行通信。

我首先展示我的代码然后描述我的问题。

这是服务器的代码:

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*;

public class server extends UnicastRemoteObject implements serverInterface
{
  public server() throws java.rmi.RemoteException {super(Registry.REGISTRY_PORT);}
  public static String MSG = "Hi from server";
  public String getMessage()  {return MSG;}
  public static void main(String args[]) throws Exception
  {
    server myServer = new server();
    LocateRegistry.createRegistry(1099);
    System.out.println("Server is up and registered");
    Naming.rebind("rmi://192.168.137.1:1099/test",myServer);
  }
}

这是服务器界面的代码:

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


public interface serverInterface extends Remote
{
  public String getMessage() throws RemoteException;
}

最后这里是客户端的代码:

import java.rmi.Naming;


public class client
{
  public static void main(String args[]) throws Exception
  {
    System.out.println("Client is up");
    serverInterface obj = (serverInterface)Naming.lookup("rmi://192.168.137.1:1099/test");
    System.out.println("Connected");
    System.out.println(obj.getMessage());
  }
}

这里到目前为止它是如何发生的,以及我被卡住的地方:

1-在同一台机器上连接(通过localhost):完美无缺。

2-通过LAN连接:

我通过MS-DOS以这种方式创建了一个托管网络:

   netsh wlan set hostednetwork mode=allow ssid=h key=0123456789
   netsh wlan start hostednetwork

通过ipconfig我看到我在这个局域网中的ip地址是192.168.137.1

我让服务器绑定到该IP并让客户端查找它,一切都运行得很好。

现在问题出在这里;我有一个对等连接到我的局域网,他的IP是192.168.137.56。启动客户端后,他收到以下错误消息:

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 192.168.56.1; nested exception is:

        java.net.ConnectException: Connection timed out: 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.invoke(Unknown Source)
        at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
        at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
        at com.sun.proxy.$Proxy0.getMessage(Unknown Source)
        at client.main(client.java:12)
Caused by: java.net.ConnectException: Connection timed out: 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)
        ... 8 more

我不知道错误报告连接到192.168.56.1的连接失败的原因,甚至连服务器的IP地址(192.168.137.1)都没有。

为了获得预期的结果,我做错了什么或者缺少了什么?

谢谢。

0 个答案:

没有答案