【问题标题】:DataOutputStream: dos.write() in loop, Receiver receives only one data packageDataOutputStream:dos.write() in loop,Receiver只接收一个数据包
【发布时间】:2019-05-16 20:28:52
【问题描述】:

我的 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();

在平板电脑上,我通过以下代码 sn-p 接收数据:

@Override
public 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();
    }
}

正如我所说,当我只发送一个数据包时,连接工作正常。但是如果我想在循环内发送多个包裹,只有第一个包裹会到达目的地。

谁能帮助我? :)

【问题讨论】:

    标签: java android sockets dataoutputstream


    【解决方案1】:

    DataOutputStream 上调用close() 会关闭其关联的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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多