got a problem in sending voice file

hey, everyone, I wrote a code that can send a file
if i send text file it will work perfectly, but when i try to send voice it only send part of the
data and then it stop sending i tried to send .wav .mp3 .m4p

the code

#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <SD.h>






const int push1 = 2;
File myFile;
char a[350];
int c= 0; 
int counter = 0 ;


// network parameters
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x05, 0x02 };  // ethernet interface MAC address
IPAddress localIp(10,131,41,72);    // local ip address
IPAddress destIp(10,131,41,74);      // destination ip address
unsigned int port = 9631;               // destination port

// EthernetUDP to send and receive messages.
EthernetUDP Udp;

// setup the arduino and shields
void setup() {

    pinMode(push1, INPUT); 
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600); // 19200
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");


  // start ethernet and udp
  Ethernet.begin(mac, localIp);   // static ip version
  
  // open UDP port
  Udp.begin(port);

  // show the local ip address (useful for dhcp)
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());



}

// do tasks
void loop() {

if(digitalRead(push1) == LOW){
    
    delay(1000);
    myFile = SD.open("sv12.wav");
     
  if (myFile) {
    Serial.println("inside the file there's");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      a[c] = myFile.read();
      c++;  

      if ( c==350 ){ // when array is full send the UDP message
      Serial.println("i will send now");
      Udp.beginPacket(destIp, port);
      Udp.write(a);
      delay(250);     
      Udp.endPacket();
      Serial.println("Sending UDP message");
       counter++; // to know how many packet is sent so far
       Serial.print("Sending packet#");
       Serial.println(counter);
         for (int x = 0 ; x<c ; x++){
 
         a[x] = '\0';}
         c=0;
      
  Serial.println("clearing");}


 
    }

          if ( c<350 ){ // to send last packet 
      Serial.println("i will send now");
      Udp.beginPacket(destIp, port);
      Udp.write(a);
      Udp.endPacket();
      Serial.println("Sending UDP message Last segment");
      counter++;
         for (int x = 0 ; x<c ; x++){
 
         a[x] = '\0';}
         c=0;}
    
    
    // close the file:
    myFile.close();
    Serial.println(counter);
    Serial.println("END");
  } else {
   // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
 }

     
}
}

can someone help please.

It would really help the readability of this post if you did an autoformat and got rid of all the superfluous blank lines from the code.

BJHenry:
It would really help the readability of this post if you did an autoformat and got rid of all the superfluous blank lines from the code.

I Tried to make the code more readable.
hope you can help

I think Udp.write(a); is your main problem but first the array should really be a byte array and not a char array and then the Udp.write should also include the size of the data you want to write else it will write out until it encounters the first zero that it takes to be the end of the char array.
Udp.write(a,c); should do it and you also don't need to clear the array after sending it as your specifying the size.

Riva:
I think Udp.write(a); is your main problem but first the array should really be a byte array and not a char array and then the Udp.write should also include the size of the data you want to write else it will write out until it encounters the first zero that it takes to be the end of the char array.
Udp.write(a,c); should do it and you also don't need to clear the array after sending it as your specifying the size.

i did it and it works but then there's another problem showed up in the receiver side
when i send 50K byte mp3 file the receiver only receives 20K byte

the receiver code

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SD.h>


int LED = 8;
int x=0;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x06
};
IPAddress ip(10,131,208,40);
IPAddress remIp(10,131,208,39);

unsigned int localPort = 9631;      // local port to listen on // 123456

File myFile;
char y[100];
int c= 0; 

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

char packetBuffer[168]; //buffer to hold incoming packet,
//UDP_TX_PACKET_MAX_SIZE
void setup()
{
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);//change and try
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());


    if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  
}

void loop()
{
  
  int packetSize = Udp.parsePacket();
   
  if (packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());


    // read the packet into packetBufffer
    Udp.read(packetBuffer, 168);
    Serial.println("Contents:");
    


    RandS();
    Serial.println(x); //to check the message

    
  }

}

void RandS(){

   myFile = SD.open("LAST12.mp3", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.println("i'm Writing what i got from UDP");
    myFile.print(packetBuffer);
    x++;
    // close the file:
    myFile.close();
    
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

}

the modify sender code

#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <SD.h>






const int push1 = 2;


File myFile;
byte a[168];
int c= 0; 
int counter = 0 ;
unsigned long data; 
unsigned long result;
unsigned long remaining ; 


// network parameters
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x05, 0x02 };  // ethernet interface MAC address
IPAddress localIp(10,131,41,72);    // local ip address
IPAddress destIp(10,131,41,74);      // destination ip address
unsigned int port = 9631;               // destination port

// EthernetUDP to send and receive messages.
EthernetUDP Udp;


// timing



// setup the arduino and shields
void setup() {

    pinMode(push1, INPUT); 
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600); // 19200
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");


  // start ethernet and udp
  Ethernet.begin(mac, localIp);   // static ip version
  //if(Ethernet.begin(mac) == 0) {     // dhcp version
  // report failure to obtain network parameters
  // Serial.println("DHCP error");

  // no point in carrying on, loop indefinitely:
  // while(true)
  // ;
  //}

  // open UDP port
  Udp.begin(port);

  // show the local ip address (useful for dhcp)
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());



}

// do tasks
void loop() {

if(digitalRead(push1) == LOW){
    
    
    myFile = SD.open("SV12.mp3");
     data = myFile.size();
 
//     result = myFile.position();

     
  if (myFile) {
    Serial.println("inside the file there's");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      a[c] = myFile.read();  
//       Serial.println(a[c]);
       c++;
      if ( c==168){
      Serial.println("i will send now");
      Udp.beginPacket(destIp, port);
      Udp.write(a,168);    
      Udp.endPacket();
      delay(1000); 
      Serial.println("Sending UDP message");
       counter++;
       Serial.print("Sending packet#");
       Serial.println(counter);
//         for (int x = 0 ; x<c ; x++){
// 
//         a[x] = '\0';}
         c=0;
      
  Serial.println("clearing");}


  
//  Serial.println(result);
    }

          if ( c<168 ){ // if (remaining)
      Serial.println("i will send now");
      Udp.beginPacket(destIp, port);
      Udp.write(a,168);
      Udp.endPacket();
      Serial.println("Sending UDP message Last segment");
//      counter++;
//         for (int x = 0 ; x<c ; x++){
// 
//         a[x] = '\0';}
         c=0;}
    
    
    // close the file:
    myFile.close();
     Serial.println("im here");
    Serial.println(counter);
    Serial.println("END");
  } else {
   // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
 }

     
}
}

A quick look at your transmit code and you have the small problem of sending a 168 byte packet for the last packet, no mater how much data is actually available. I think the problem is your not reading all of the packets and end up missing data.
A quicker look at the RX code and your only storing data upto a '0' char terminator with your myFile.print(packetBuffer);, instead of the full buffer.

You have a buffer of 168 bytes length but the max UDP packet size (UDP_TX_PACKET_MAX_SIZE) is 24 so you gain little from having a bigger buffer

As your using UDP I think you need to setup a protocol so the TX data includes at least the packet number being sent and the RX code should acknowledge the received packet back to TX so it can either continue with the next packet or re-transmit the same packet again.