0

i have problem while sending files to my server. The main problem is the protocol. First im sending with an PrintWriter the file Size, and than im using a DataOutputStream to write the binary data to my server. Everything works fine, but there is one problem. Im using the method File.length() to get the file length. The Docs say, that its possible to read less bytes-length as the method return. And thats what happening sometimes. So after this, the while loop in my server which is reading data will never exit, because it waiting for the last bytes of data. I tried everything, like readfully() on client side. Nothing worked. Have anyone a idea how to stop this while loop in an other way? Like waiting for specific byte that tells the Server to stop it, because there is no more data to read? I dont really know how to implement a communication protocol.I searched everywhere for information but i found nothing that helped.

Why i dont closing the connection to exit the while loop? I need to use this connection later, to tell the client that the file was uploaded succesfully.

Client: Sending file length = 1000
Server: Reading file length
Client: Sending byte data ...
Server: Reading byte data ...
Client: Finished sending byte data, written only 900
Server: Still waiting for data, because it dont reached the excepted length.

private boolean uploadPicture(File file) throws Exception
        {
            int maxLength = Integer.parseInt(this.readDataCarefully(10)); //read until \n appears
            int readed = 0;

            if(maxLength >= 500000)
            {
                return false;
            }

            DataInputStream dateiInputStream = null;
            FileOutputStream dateiStream = null;
            dateiInputStream = new DataInputStream(this.socket.getInputStream());
            dateiStream = new FileOutputStream(file);

            int count;
            byte[] buffer = new byte[maxLength];
            while ((count = dateiInputStream.read(buffer)) > 0)
            {
                dateiStream.write(buffer, 0, count);
                readed = readed+count;
                if(maxLength <= readed) //True when file transfere complete.
                {
                    break;
                }
            }   
            System.out.println("Finished");
            dateiStream.close();

            if(this.checkImageFile(file)) //Checks file
            {
                System.out.println("PB WRITTEN.");
                return true;
            }
            return false;
        }
  • You should reassess how you ascertain the size, rather than trying to find a signal which would be easy to misinterpret. – Joe C Sep 24 '17 at 19:54
  • @JoeC But how could i get the file length of a file without using File.length(). I tried to read the file in a bytearrayouputstream and read the size from it. But there was the same problem. –  Sep 24 '17 at 20:43
  • Well, one of two things is happening here. Either the file size you're sending is wrong, or bytes are being dropped somewhere. You should spend some time in a debugger to work out which is the case. – Joe C Sep 24 '17 at 20:45
  • @JoeC Spending hours of hours on this problem. And i can tell yout that the problem is the file size. Because it transmitted files correctly if the file size was correct. And i read the docs and they said, that fileinputstream.read() read so many bytes as possible. But it can happen that it wont read the expected file legth from File.length(). Or you have another idea how to tell the server that no more data will be sent. –  Sep 24 '17 at 20:50
  • There are several problems here: mixing buffered and unbuffered streams on the same socket; overrunning the transmitted file; ... See my answer in the duplicate. – user207421 Sep 24 '17 at 22:25

0 Answers0