Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi,

I am trying to send a video file from Android to C using tcp/ip protocol. When android sends a video file, i am able to receive the file, but when i play the file the file is not getting played and moreover if the actual file plays a video for 2 minutes after receiving using tcp/ip the video size is about 3 hours 5 minutes and so on...

C#
long long int receive_text(long long int socket)

{

   long long int buffersize = 0, recv_size = 0, size = 0, read_size, write_size;

   char textarray[8196000],errno;

   char verify[15] = "text Received";

   FILE *text;

   //Find the size of the text

   read(socket, &size, sizeof(int ));

   //Send our verification signal

   write(socket, &verify,sizeof(char));

   //Make sure that the size is bigger than 0

   /*if(size <= 0 )

   {

           printf("Error has occurred. Size less than or equal to 0 \n");

           return -1;

   } */

       text = fopen("/home/sosdt009/Desktop/receivedandroid.mp4","w");

       if( text == NULL)

   {

           printf("Error has occurred. text file could not be opened \n");

           return -1;

       }

       //Loop we have not received the entire file yet

       while(recv_size < size)

       {

               ioctl(socket, FIONREAD, &buffersize);

           //We check to see if there is data to be read from the socket

           if(buffersize > 0 )

   {

            if(read_size = read(socket, textarray, buffersize) < 0){

        printf("%s", strerror(errno));

       }

            //Write the currently read data into our text file

           write_size = fwrite(textarray,1, buffersize, text);

      /* if(write_size != buffersize)

       {

         printf("write and buffer sizes are wrong \n");

       }

       if(read_size != write_size)

       {

           printf("error in read write \n");

       }  */

       //Increment the total number of bytes read

      recv_size += read_size;

       //Send our handshake verification info

   write(socket, &verify,sizeof(char));

   if (read_size == write_size)

               {

                   printf("%lld,%lld", read_size, write_size);
                   printf("text successfully Received! \n");

                   }

           else

               {

                   //sleep(1000);
                   printf("%lld,%lld", read_size, write_size);
                   printf(" Data Received successfully: \n");

               }


     }

     }

   fclose(text);

   return 0;

}
Posted

1 solution

the most obvious problem is you ignore the size of the chunk your read (read_size) and always write buffersize bytes to the file
 
Share this answer
 
Comments
venkat28vk 20-Apr-15 4:33am    
what can i do? Can you suggest how to do achieve this
barneyman 20-Apr-15 8:12am    
change the fwrite to only write what you've read ... review the documentation on read and fwrite, fix the code, then run it under a debugger
venkat28vk 20-Apr-15 8:44am    
I have used write as per you said, but still it is not working. Are there any other way for resolving this?
barneyman 20-Apr-15 21:06pm    
run it under a debugger, its only ~30 lines of code, and it's not complex - it should be easy to work out where it's going wrong

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900