【问题标题】:inputStream read method constantly reading 0 [duplicate]inputStream读取方法不断读取0 [重复]
【发布时间】:2016-05-08 16:32:55
【问题描述】:

我首先将一个文件从客户端传输到我的主机,存储字节数组,然后发送到从机。从站存储字节数组的位置。但是当文件从客户端正确发送到主服务器时,但是当我将字节数组发送到从服务器时,它会发送到从服务器,输入流中的读取方法不断读取 0。

//该方法将文件写入master

public void writeFile(File file) {
    try {
        this.write(String.valueOf(file.length()));

        byte[] bytearray = new byte[(int) file.length()];
        FileInputStream fin = new FileInputStream(file);
        BufferedInputStream bin = new BufferedInputStream(fin);
        bin.read(bytearray, 0, bytearray.length);
        BufferedOutputStream bos;
        OutputStream os = socket.getOutputStream();
        bos= new BufferedOutputStream(os);
        bos.write(bytearray, 0, bytearray.length);
        bos.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

//该方法将文件作为字节数组读入master,字节数组从master读入slave

public byte[] readFile() {

    byte[] bytearray = null;
    try {
        int currentTot = 0;
        int filesize = Integer.parseInt(this.read());
        System.out.println(filesize);
        bytearray = new byte[filesize];
        InputStream is = socket.getInputStream();
        int bytesRead;

        bytesRead = is.read(bytearray, 0, bytearray.length);
        currentTot = bytesRead;
        int count = 0;
        do {
            bytesRead = is.read(bytearray, currentTot, (bytearray.length - currentTot));
            if (bytesRead > 0) {
                currentTot += bytesRead;
                count = 0;
            } else {
                count++;
                System.out.println("count " + count);
            }
        } while (bytesRead > -1);
        System.out.println(currentTot);
        // bos.write(bytearray, 0, currentTot);
        // bos.flush();
        // bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bytearray;
}
//This method writes from the master to the slave 
public void writeByte(byte[] m) {
        this.write(String.valueOf(m.length));
        System.out.println("File side inside sender" + m.length);
        // byte[] bytearray = m;
        OutputStream os;
        try {
            os = socket.getOutputStream();
            os.write(m, 0, m.length);
            os.flush();
            //os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

有趣的是,如果我在从我的主人发送我的字节数组后关闭我的输出流,它工作得很好。但是我无法关闭流,因为从站需要进一步与主站通信。提前致谢。

public void write(String output) {
    if (pw == null)
        this.openWriter();
    pw.println(output);
}
public String read() {
    try {
        if (br == null) {
            if (this.socket != null)
                br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        }
        return br.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

【问题讨论】:

    标签: java tcp bytearray outputstream


    【解决方案1】:

    您误读了接收器中的文件长度。你得到了零,所以你正在构造一个长度为零的字节数组,所以read() 返回零。

    您需要通过DataOutputStream.writeLong() 发送长度并通过DataInputStream.readLong() 读取它。然后您的发送和接收代码也都是错误的。完整代码见my answer here

    【讨论】:

    • 不,我测试过。正在读取整个 byteArray。我放了断点并检查了。它只是字节数组的最后一块没有通过
    • 我也检查了你的答案,我不知道你为什么需要读取到 fileSize>0,因为读取的 Javadoc 明确指出,当流中没有任何内容时,它将返回 -1。我尝试使用该解决方案,当我使用流将字节数组从我的主服务器发送到其中一个从服务器时,现在未读取的最后一位被作为额外的推送到从服务器。
    • 是的,您误读了文件长度。您将其作为字符串发送,并且只读取其中的一个字节。 read() 可以返回零的唯一方法是您提供零长度。引用的答案是关于发送多个文件的问题,其中流的结尾不能用于分隔发送的文件。
    • this.read 是对读取字符串的类的方法的调用,this.write 是对写入字符串的类的方法的调用。更新了上面的代码以反映这一点。 Input]Streams read() 方法的 Javadoc 声明,当没有任何内容可读取时,它返回 -1。
    • 如果有更多涉及此问题的代码,您应该提供它。 read() 如果您提供零长度,则返回零,如果您到达流的末尾,则返回 -1。您提供的长度为零。引用答案中的代码有效。您问题中的代码没有。我不知道你为什么要为此争论。
    猜你喜欢
    • 1970-01-01
    • 2010-11-18
    • 2012-01-17
    • 1970-01-01
    • 2011-08-10
    • 2010-12-25
    • 2018-10-24
    • 2020-10-28
    • 2010-12-20
    相关资源
    最近更新 更多