簡體   English   中英

BufferedReader未通過套接字接收消息

[英]BufferedReader not receiving message over socket

情況是有4個“幫助程序”線程,每個線程都與所有幫助程序(包括其自身)連接。 我確實在自動沖洗。 下面的this.id是一個int,用於標識運行代碼的幫助程序。 在調試中,我已經看到輔助程序0向其自身發送一條消息(通過所有連接發送),但是當它應該接收該消息(通過連接0)時,它的行為就像其BufferedReader具有空緩沖區。

解決:糟糕,我有一個設計缺陷。 我確保每個助手需要與之通信的每個助手僅保留一個套接字,但是當助手連接到其自身時,這是不正確的。 在這種情況下,僅保留一個套接字意味着(與自身的)另一通信端點將丟失。

我的程序在這里永遠旋轉(必須為空緩沖區):

        boolean clientStreamReady = false;
        boolean helperStreamReady = false;
        while (!(clientStreamReady || helperStreamReady)) {
            clientStreamReady = this.inFromClient.ready();
            helperStreamReady = this.helperIns.get(this.id).ready();

            if (clientStreamReady) {
                message = this.inFromClient.readLine();
            }
            else if (helperStreamReady){
                message = this.helperIns.get(this.id).readLine();
            }
        }

該消息在此處為“廣播”:

    for (PrintWriter out : this.helperOuts) {
        out.println(this.id + "," + kind + "," + this.clock.getTime());
    }

在上面進行設置的套接字的IO在這里完成:

    for (Socket socket : this.helperSockets) {
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            this.helperOuts.add(out);
            this.helperIns.add(in);
    }

如果需要套接字設置代碼,請告訴我,我將其添加。

您的PrintWriters是否啟用了自動刷新? 如果沒有,在廣播循環末尾的簡單out.flush()可能已經解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM