为什么我的连接器没有收到主机的消息?

问题描述 投票:-1回答:1

Summary

我正在用Java制作一个简单的点对点国际象棋游戏。主机成功连接,但连接器未收到其消息。我正在使用PrintWriterBufferedReader发送和接收消息。我怀疑PrintWriter行为不端,但我不确定。

Prior research

我搜索了“客户端没有收到服务器的消息”,但每个人都遇到的问题是没有使用println和他们的PrintWriter。我确实使用println,所以没有一个适用于我。我还将输入和输出字段移出方法并进入类,一个答案说,但它没有解决问题。

Some code

Listening code

try (ServerSocket serverSocket = new ServerSocket(this.port)) {
    // We need to connect with another computer
    if (this.other == null || this.other.isClosed()) {
        System.out.println("Looking for connection on port " + serverSocket.getLocalPort());
        this.in = null;
        this.out = null;
        // Listen for connectors
        this.other = serverSocket.accept();
        // Someone tried to connect, handle connection
        System.out.println("Player connected");
        this.in = new BufferedReader(new InputStreamReader(this.other.getInputStream()));
        this.out = new PrintWriter(this.other.getOutputStream(), true);
        // Autoflush is enabled!                                 ^^^^
        // This does not get to the client
        this.sendMessage("connectHost");
    }
    // We are currently connected to another computer, no need to look for more
    else {
        String input = this.in.readLine();
        System.out.println("Received '" + input + "'");

        if (input != null) {
            // Handle input
        }
    }
} catch (IOException ioe) {
    ioe.printStackTrace();
}

Sending code

if (this.out != null) {
    // See, it is println, so I don't need to call out.flush()...
    this.out.println(message);
    System.out.println("Sent '" + message + "'");
}

Connection command code

try {
    // The host picks up on this
    this.other = new Socket(ip, port);
    this.in = new BufferedReader(new InputStreamReader(this.other.getInputStream()));
    this.out = new PrintWriter(this.other.getOutputStream(), true);
    // Works
    this.sendMessage("test");
} catch (IOException ioe) {
    ioe.printStackTrace();
}

Problem

它应该在连接器上打印出Received 'connectHost',但这不会发生。相反,它阻止了in.readLine()被调用而没有接收数据的in。如果我插入in.ready()检查,它将始终返回false

Connector console

Looking for connection on port 57479
connect localhost 57478 // Command to connect to host
Sent 'test' // Successfully goes to the host
// Now it blocks. This is intended, but it should be saying "Received 'connectHost'" beforehand.

Host console

Looking for connection on port 57478
Player connected
Sent 'connectHost'
Received 'test' // Host gets it
// This also blocks, waiting for more messages, but that is intended.

更新:我刚刚尝试在连接后直接发送消息(请参阅更新的代码)并且主机获取它。连接器仍然无法从主机获取连接器。

java sockets
1个回答
0
投票

It was obvious...

serverSocket.accept()阻塞,阻止连接器读取输入。所以in.readLine()不是问题。我做到了,所以用户必须告诉程序监听连接。现在它有效。

新代码

Listening code

try (ServerSocket serverSocket = new ServerSocket(this.port)) {
    if (this.listening && (this.other == null || this.other.isClosed())) {
        System.out.println("Looking for connection on port " + serverSocket.getLocalPort());
        this.in = null;
        this.out = null;
        this.other = serverSocket.accept();
        this.in = new BufferedReader(new InputStreamReader(this.other.getInputStream()));
        this.out = new PrintWriter(this.other.getOutputStream(), true);
    } else if (this.in != null) {System.out.println("Looking for input");
        String input = this.in.readLine();
        System.out.println("Received '" + input + "'");

        if (input != null) {
            // Process input
        }
    }
} catch (IOException ioe) {
    ioe.printStackTrace();
}

发送代码没有变化。

Connection code

if (input.contains("connect")) {
    String[] args = input.replaceAll("connect ", "").split(" ");
    String ip = args[0];
    int port = Integer.parseInt(args[1]);

    try {
        this.other = new Socket(ip, port);
        this.in = new BufferedReader(new InputStreamReader(this.other.getInputStream()));
        this.out = new PrintWriter(this.other.getOutputStream(), true);
        this.sendMessage("connect");
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
} else if (input.contains("listen")) {
    this.listening = true;
}

推荐问答