1 2 Previous Next 20 Replies Latest reply: Mar 10, 2014 11:39 AM by gimbal2 Go to original post RSS
      • 15. Re: How to send a file without closing the socket?
        Karthik Aryan

        No, both the peers are Android. In my SSCCE code, i have 2 buttons one to start the server and the another to start the client. so this code should be run by two individual android devices which connected two same network and the ip address of the server should be same in the client code. So when you start the server from a device it will be ready to accept a client and then you should start the client, as soon as it get started tcp connection made and the "test.txt" file present in the sdcard on the server device will be send to the client.

         

         

        But i found my problem as posted the stackoverflow link above with the code will tell you.. That is sender reads the file fully but while receiving around 10KB is missing that too present at the beginning of the packets sent. In the link they said that the error occur is due to header and body and my guess is that in the receiver side the header is not read.

         

        As i said it is only my guess and also if it so, i don't know how to gid rid of that.

        • 16. Re: How to send a file without closing the socket?
          baftos
                             BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                            String fname = rd.readLine();
                             System.out.println("Recieved: " + fname);
                            String len=rd.readLine();
                            String flen="";
                            flen=len.substring(len.indexOf('&')+1, len.lastIndexOf('&'));
                            long fl=Long.parseLong(flen),ch=0;
                            System.out.println("FileSize : "+fl);
                            File folder = new File(Environment.getExternalStorageDirectory () + "/WiFi");
                            if(!folder.isDirectory())
                               folder.mkdirs();
                             rd.close();
                              FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory () +"/WiFi/"+fname);
                            BufferedOutputStream bos = new BufferedOutputStream(fos);
                            int read_count=0;
                            byte buffer[] = new byte[1024];
                            long start = System.currentTimeMillis();
                              while(ch<=fl) {
                                  read_count=is.read(buffer);
          
          
          

          You are right that your problem is similar to the one at Stack Overflow. Let's look at the code above. You have a BufferedReader that is supposed to help you with reading the file name and size as two separate lines. Its buffer is, let's say 1000 bytes. Your two lines are, let's say 100 bytes in total. After doing this, you start reading directly from the socket stream, which already delivered 1000 bytes, 900 of them from the file contents. So 900 bytes are lost! But BufferedReader has a read(byte[] buffer) method. Therefore, if you continue using the BufferedReader instead of the socket  input stream, everything should be ok, I think. The BufferedReader knows that it delivered the two lines of data only and when read(byte[] buffer) comes, it will continue with the file contents.

          • 17. Re: How to send a file without closing the socket?
            EJP

            Such a long thread, so many errors.

             

            > Send an end-of-file marker after each file

             

            That cannot possibly work unless either the EOF marker cannot occur within the file, which is unknowable, or there is an agreed escape convention in case it does occur within the file. In which case you also need to apply the escape convention to the escape convention. For example, you could terminate with ETX and escape with ESC, but that means that both ETX and ESC need to be escaped within the file data, and that in turn means you need to de-escape ESC ETX and ESC ESC when receiving.

             

            > In the link they said that the error occur is due to header

             

            No, in the link they said the error is due to using a BufferedReader and then reading from the stream directly. Part of the stream disappears into the BufferedReader's, err, buffer.

             

            > BufferedReader has a read(byte[] buffer) method

             

            No, BufferedReader does not have a read(byte[] buffer) method.

             

            Tested, unspeculative answer with code follows.

             

            1. Use DataOutputStream at the sender, and DataInputStream at the receiver.

            2. Send the file name with writeUTF() and read it with readUTF().

            3. Send the file length with writeLong() and read it with readLong().

            4. Send and receive exactly that many bytes, as follows:

            long length = din.readLong(); // in the receiver, or File.length() in the sender

            long total = 0;

            int count;

            byte[] buffer = new byte[8192]; // or whatever you like, any size > zero

            while (total < length && (count = din.read(buffer, 0, (int)Math.min(buffer.length, length-total))) > 0)

            {

                dout.write(buffer, 0, count);

                total += count;

            }

            Errors and omissions excepted. You can use this code at both ends.

            • 18. Re: How to send a file without closing the socket?
              baftos

              EJP wrote:

               

              No, BufferedReader does not have a read(byte[] buffer) method.

               

               

              Oh well, my diagnostic was ok, but my treatment was bad. Sorry...

              • 19. Re: How to send a file without closing the socket?
                Karthik Aryan

                Thank you very much it works great!

                 

                But i want to know why while using inputstream/outputstream it is not receiving correctly?

                • 20. Re: How to send a file without closing the socket?
                  gimbal2

                  I think that has been explained a few times already; read EJP's -entire- response.

                  1 2 Previous Next