checksum
That's one way, but if the data is just completely invalid like, we're sending pascal style length-prepended strings, and I say the length of the string i'm sending is 10,000,000,000 and your program sits there waiting for 10,000,000,000 bytes, then that's simple denial of service. You need to design your program to deal with that.