0

I am trying to send a file with Java TCP socket I/O Stream from Client to Server. I am using Data(Input|Output)Stream in this process.

With every packet of file buffer, I want to send extra information (a Long) as HEADER to every buffer. I intend to use that info later for controlling I/O Streams.

Everything Works fine for the Following Codes (Without sending extra info (the long)):

Client : out is DataOutputStream from socket OutputStream

            byte[] buffer = new byte[BUFFER_SIZE];
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(data.getFile()));

            out.writeUTF(file.getName());
            out.writeLong(file.length());

            int n;
            long currentProgress = 0, totalProgress = file.length();

            while ( (n =in.read(buffer, 0, buffer.length) ) != -1) {
//                out.writeLong(123456);
                out.write(buffer, 0, n);
                out.flush();

                currentProgress += n;
            }

            in.close();

Server : in is DataInputStream from socket InputStream

        String fileName = in.readUTF();
        long fileSize = in.readLong();

        File file = new File(fileName);

        try {
            file.getParentFile().mkdirs();
        } catch (NullPointerException ignore) {}

        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));

        byte[] buffer = new byte[BUFFER_SIZE];
        int n;

        for (long sizeToRead = fileSize;
                     sizeToRead > 0; sizeToRead -= n) {

//            in.readLong();
            n = in.read(buffer, 0, (int) Math.min(buffer.length, sizeToRead));
            if (n==-1) break;


            bufferedOutputStream.write(buffer, 0, n);
        }

        bufferedOutputStream.flush();
        bufferedOutputStream.close();

Everything just works fine now, but when I uncomment these two lines:

// out.writeLong(123456);

// in.readLong();

(Big) files get corrupted, I do not understand why this is happening as I am sending and receiving in order.

BUFFER_SIZE is currently 512kb (512*1024)

[This is my first question in StackOverflow, sorry if I have represented my question poorly, Thank You]

Sabit
  • 1
  • 2
  • ***(Big) files get corrupted*** What's a "big" file? How do you know you're passing the file size properly? Have you verified the size is correct on the receiving side? – Andrew Henle Jun 30 '20 at 00:21
  • I don't see why it shouldn't work, as long as you deploy the code correctly, but why on earth *are* you writing 123456 in the first place? This is the time to send data, not constants, or even `long` variables. NB You don't need such enormous buffers. You'll find that 8192 doesn't make it perceptibly slower. Don't flush inside loops, and flush before close is redundant. – user207421 Jun 30 '20 at 00:56
  • @MarquisofLorne Sending 12345 was just a test, if the test had been passed, I would have continued. Thanks for the advice of not using flush inside loop. – Sabit Jun 30 '20 at 01:21
  • @AndrewHenle & MarquisofLorne I think I have found the problem. After running the program through debugger I found that > Cycle of READ and WRITE is not same Read Cycle much less than the write cycle. As loops continues, readLong continues reading from the buffer which eventually corrupts data from from file (missing data). Thanks everyone. – Sabit Jun 30 '20 at 01:27

0 Answers0