I wrote a UDP based transfer protocol using C ++ 11 (VS2013). It flashes quickly - and works fine 99.9% of the time.

But I noticed several times that the wrong bytes are written to disk (Samsung 250 GB SSD 850 EVO) - or at least it seems to be so.
Here, basically, something happens when I transfer the test file to 6 GB:
- The file is split into smaller UDP data packets - 64 KB in size. (The network layer disables and reassembles UDP datagrams into a larger packet).
- The client sends datapackage (udp) to the server - the payload is encrypted using AES256 (OpenSSL) and contains + data metadata. The payload also contains SHA256 hashing of the entire payload - replenish the UDP checksum as an additional integrity check.
- The server receives the data packet, sends the "ACK" packet back to the client, and then calculates the SHA256 hash. The hash is identical to the hash client - everything is fine
- The server then writes the packet data to disk (using fwrite instead of streams due to huge performance differences). The server processes only one packet at a time - and each pointer file has a mutex protector that protects it from being closed by another worker thread, which closes file pointers that have been inactive for 10 seconds.
- The client receives UDP "ACK" packets and resends packets that were not allocated (this means that they did not). Incoming ACK packet rate controls client send rate (e.g. congestion control / throttling). The order of the packets received on the server does not matter, since each packet contains the Position value (where the data should be written in the file).
After transferring the entire file, I make a full SHA256 hash of a 6 GB file both on the server and on the client, but, to my horror, I observed twice in the last few days that the hash is NOT the same (when performing about 20 test transfers )
After comparing files in Beyond Compare, I usually find that there is one or two bits (in a 6 GB file), which is incorrect on the server.
See screenshot below: 
Server code - called after checking the DataPackage hash
void WriteToFile(long long position, unsigned char * data, int lengthOfData){ boost::lock_guard<std::mutex> guard(filePointerMutex);
So to summarize:
- char * was right in memory on the server - otherwise the SHA256 Hash servers would fail - right? (a hash collision with sha256 is highly unlikely).
- Corruption seems to occur when writing to disk. Since about 95,000 of these 64-kilogram packets are written to send a 6 GB file, and this happens only once or twice (when it happens at all) - this is a rare occurrence
How can this happen? Is my hardware (bad ram / drive) to blame for this?
Do I need to read from disk after writing and do, for example, memcmp to be 100% sure that the correct bytes are written to disk? (Oh boy, what a defeat will be ...)
source share