首页 > 解决方案 > 为什么我尝试在另一台计算机上运行 Client.jar 但收到连接被拒绝”或“连接超时

问题描述

我花了一天多的时间,但仍然无法弄清楚我该如何解决这个问题。每次我关闭服务器上的防火墙时,我的另一台计算机都可以成功运行Client.jar。

我检查了我的服务器 PC 上的防火墙。我确定我打开了“4888”和“6151”端口,但仍然无法正常运行。

我不知道“W.getSocketFactory()”是如何工作的。

System.out.println("創造"+ W.getSocketFactory());  → 創造 null  (Is it an issue?)

我的代码有问题吗?

下面是我的服务器代码:

public class RemoteServer extends UnicastRemoteObject implements MyRemote{
    public static void main(String[] args)
    {
        try {
            RMILocalSocketFactory W = new RMILocalSocketFactory(6151);
            System.out.println("創造"+ W.getSocketFactory());
            RMISocketFactory.setSocketFactory(W);
            
            LocateRegistry.createRegistry(4888);
            System.setProperty("java.rmi.server.hostname","175.183.49.139");
            MyRemote Server = new RemoteServer();           
            Naming.rebind("rmi://175.183.49.139:4888/Remote_Hello!", Server);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    RemoteServer() throws RemoteException {}
    
    public String SayHello() 
    {       
        System.out.println("RMI Connection");
        return "you Connected Server\n" + "Server says , Hey!";
    }
}
class RMILocalSocketFactory extends RMISocketFactory {

    private int dataPort;
    
    public RMILocalSocketFactory(int dataPort1){
        this.dataPort = dataPort1;
    }

    public Socket createSocket(String host, int port) throws IOException {
        String a = "175.183.49.139";
        Socket check = new Socket(a = host, port);
        return check;
    }

    public ServerSocket createServerSocket(int port) throws IOException {   
        return new ServerSocket(port);
    } 
} 

(客户端)代码的一部分:

try {
    LocateRegistry.getRegistry(4888);
     
    MyRemote Service = (MyRemote)Naming.lookup("rmi://175.183.49.139:4888/Remote_Hello!");
    String Say= Service.SayHello();
    Area.append(Say);
} catch (Exception e) {
    e.printStackTrace();
}
```[Problem Picture:][1]


  [1]: https://i.stack.imgur.com/LkK5T.jpg

标签: javarmi

解决方案


我自己解决了我的问题。

我终于发现我在防火墙上的 java.exe 被阻止了。

这就是为什么在防火墙打开时我无法收到另一个请求的原因。

并确保两个端口也必须在防火墙上打开。


推荐阅读