0

To send and receive file via socket over Wifi I have used following code...

Client side :

Socket socket = new Socket(dstAddress, dstPort);
            int bufferSize=socket.getReceiveBufferSize();
            InputStream in=socket.getInputStream();
            DataInputStream clientData = new DataInputStream(in);
            String fileName = clientData.readUTF();
            System.out.println(fileName);
            OutputStream output = new FileOutputStream("/sdcard/"+fileName);

            byte[] buffer = new byte[bufferSize];
            int read;
            while((read = clientData.read(buffer)) != -1){
                output.write(buffer, 0, read);
            }


            //close every thing
            output.flush();
            output.close();
            socket.close();

Server side:

File file = new File(Environment.getExternalStorageDirectory(), "/sdcard/test.amr");

            byte[] mybytearray = new byte[8092];
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            DataInputStream dis = new DataInputStream(bis);
            OutputStream os;
            try {
                os = socket.getOutputStream();
                DataOutputStream dos = new DataOutputStream(os);
                dos.writeUTF(file.getName());
                dos.writeLong(mybytearray.length);
                int read;
                while((read = dis.read(mybytearray)) != -1){
                    dos.write(mybytearray, 0, read);

                }
                os.flush();

                os.close();
                socket.close();

At this point I am receiving file 'test.amr' from server without change of its original size. But when I try to play the file in client device it can't be played.

Note : mp3, avi and txt file received using above code can be opened and played perfectly.

Please suggest how to solve this issue.

Shahed
  • 335
  • 3
  • 10

2 Answers2

0

Finally I have solved the above problem by just removing dos.writeLong(mybytearray.length); line form server code... as per suggestion of greenapps.

My revised server code is:

File file = new File(Environment.getExternalStorageDirectory(), "/sdcard/test.amr");

        byte[] mybytearray = new byte[8092];
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        DataInputStream dis = new DataInputStream(bis);
        OutputStream os;
        try {
            os = socket.getOutputStream();
            DataOutputStream dos = new DataOutputStream(os);
            dos.writeUTF(file.getName());
            int read;
            while((read = dis.read(mybytearray)) != -1){
                dos.write(mybytearray, 0, read);

            }
            os.flush();

            os.close();
            socket.close();
Shahed
  • 335
  • 3
  • 10
  • You should not have posted this as an answer yourself. And you should first have answered my questions. That the line had to be removed you could read from my first comment already. You found help but do not give credit in this way. – greenapps Apr 10 '16 at 09:17
0

You're writing the buffer size as a long but you're not reading it. So the long is being read as part of the image. As writing your sending buffer size has no conceivable relevance to the receiver, you should remove the writeLong(mybytearray.length) call from the sender.

user207421
  • 305,947
  • 44
  • 307
  • 483