The receiveData
function is perfectly fine: It writes the received bytes to the buffer, and returns the number of bytes that were received.
All other bytes in the buffer can and should be ignored.
In your current code, each time you receive data, you're writing the entire buffer to the file, even though receiveData
tells you precisely how much data you should write.
Namely, you shouldn't do
int written = fwrite(mfcc, 1, FileSize, fp);
but instead
int written = fwrite(mfcc, 1, Received, fp);
You should consider using a more reasonable buffer size, such as 1500 bytes (the usual MTU for network packets), or 1MB (something that should fit into the RAM without issues), instead of the full filesize.
By the way, there is no need to pass data
as a double pointer, or, as you call it, as a reference to a pointer. Just pass it as a normal pointer. But that has nothing to do with your 'garbage data' issue.