Java RMI客户端无法连接到服务器

时间:2018-08-29 13:32:00

标签: java server rmi tic-tac-toe rmiregistry

我正在尝试实施TicTacToe游戏。为此,我有一个远程接口,远程接口实现和一个主服务器类。对于客户端,我有一个客户端类。

当客户端尝试通过用户指定的端口连接到服务器时,我遇到了一个问题。

java.rmi.ConnectException: Connection refused to host: 172.20.143.65; 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 ttt.Game.<init>(Game.java:18)
at ttt.Game.main(Game.java:67)

这是服务器端用于接收rmiregistry端口的代码

public static int registryPort = 1099;

public static void main(String args[]) {

    try {
        TTT ttt = new TTT();
        System.out.println("Enter port number");
        Scanner sc = new Scanner(System.in);
        registryPort = sc.nextInt();
        //Runtime rt = Runtime.getRuntime();
        //Process pr = rt.exec("start rmiregistry " + registryPort);
        Registry reg = LocateRegistry.createRegistry(registryPort);
        reg.rebind("TTT", ttt);
        System.err.println("Server up");
        System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

以下是客户端上用于连接的代码

   public Game() throws MalformedURLException, RemoteException, NotBoundException{
    game = (TTTService) Naming.lookup("TTT");
    keyboardSc = new Scanner(System.in);
}

该错误正在客户端代码中引发。无法找出问题所在。当我将端口设置为rmi默认值1099时,它可以工作。客户端是否缺少某些内容?

0 个答案:

没有答案
相关问题