java - inputStream读取方法不断读取0

标签 java tcp arrays outputstream

我首先将文件从客户端传输到我的主设备,存储字节数组,然后发送到从设备。从机存储字节数组的位置。但是,当文件从客户端正确发送到主设备时,但当我将字节数组发送到从设备时,它发送到从设备时,输入流中的读取方法不断读取 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;
}

最佳答案

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

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

关于java - inputStream读取方法不断读取0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102158/

相关文章:

java - GAE + Cloud Storage + Guava = Appspot 上的 NoSuchMethodError?

c++ - 如何解析 TCP 数据包负载

Java ObjectInputStream挂了

数组:找到最小交换次数以使数组的双调性最小?

java - 你知道哪些java报表框架?

java - 带有新初始化类变量的 Mockito InjectMocks

java - 两个 double 组相等

java - 多维数组对象内存位置

java - mac 上 gridbaglayout 中 JTextArea 的高度

shell - 使用 netcat 将命令发送到套接字