亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

DataOutputStream: dos.write() in loop, Receiver

DataOutputStream: dos.write() in loop, Receiver

嚕嚕噠 2022-11-30 16:52:39
我的 TCP 連接有問題。我通過智能手機通過 TCP 套接字連接將數據(一個簡單的字符串)發送到平板電腦。連接工作正常,數據按預期傳輸。但是當我做一個循環并且在每次迭代中 dos.write() 被觸發時,只有一個包到達平板電腦數據接收器。我究竟做錯了什么?這是我連接的發送部分。它遍歷列表并將每個數據寫入DataOutputStream.for(int i = 0; i <= logList.length - 1; ++i){    String backupPayload = invertLogStringToJson(logList[i]);    dos = new DataOutputStream(s.getOutputStream());    dos.writeUTF(backupPayload);    dos.flush();    dos.close();在平板電腦上,我通過以下代碼片段接收數據:@Overridepublic void run() {    try {        while(true){            mySocket = ss.accept();            dis = new DataInputStream(mySocket.getInputStream());            message = dis.readUTF();            handler.post(() -> {                bufferIntentSendCode.putExtra("data", message);                ctx.sendBroadcast(bufferIntentSendCode);            });        }    } catch (IOException e) {        e.printStackTrace();    }}正如我所說,當我只發送一個數據包時,連接工作正常。但是如果我想在循環內發送多個包裹,只有第一個包裹會到達目的地。誰能幫我?:)
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

調用close()aDataOutputStream會關閉其關聯OutputStream的 ,關閉套接字OutputStream也會關閉套接字。這是記錄在案的行為。


但是,那應該沒問題,因為您的接收器代碼無論如何只希望接收 1 個字符串。每個 TCP 連接只調用dis.readUTF()一次。


如果要在單個連接中發送多個字符串,請不要dos.close()在發送端調用(至少在發送完所有字符串之前),并且dis.readUTF()在接收端循環調用直到接收到所有字符串。


dos = new DataOutputStream(s.getOutputStream());


for(int i = 0; i < logList.length; ++i){

    String backupPayload = invertLogStringToJson(logList[i]);

    dos.writeUTF(backupPayload);

}

dos.flush();


dos.close();

@Override

public void run() {

    try {

        while (true) {

            mySocket = ss.accept();

            dis = new DataInputStream(mySocket.getInputStream());


            try {

                while (true) {

                    message = dis.readUTF();

                    handler.post(() -> {

                        bufferIntentSendCode.putExtra("data", message);

                        ctx.sendBroadcast(bufferIntentSendCode);

                    });

                }

            } catch (IOException e) {

            }


            dis.close();

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

}

或者,在發送實際字符串之前發送列表長度,然后在讀取字符串之前讀取長度:


dos = new DataOutputStream(s.getOutputStream());


// maybe other things first...


dos.writeInt(logList.length);

for(int i = 0; i < logList.length; ++i){

    String backupPayload = invertLogStringToJson(logList[i]);

    dos.writeUTF(backupPayload);

}

dos.flush();


// maybe other things next...


dos.close();

@Override

public void run() {

    try {

        while (true) {

            mySocket = ss.accept();

            dis = new DataInputStream(mySocket.getInputStream());


            try {

                // maybe other things first...


                int length = dis.readInt();

                for (int i = 0; i < length; ++i) {

                    message = dis.readUTF();

                    handler.post(() -> {

                        bufferIntentSendCode.putExtra("data", message);

                        ctx.sendBroadcast(bufferIntentSendCode);

                    });

                }


                // maybe other things next...


            } catch (IOException e) {

            }


            dis.close();

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

}


查看完整回答
反對 回復 2022-11-30
  • 1 回答
  • 0 關注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號