What is the right approach to checksumming UDP packets
I'm building UDP server application in C#. I've come across a packet checksum problem.
As you probably know, each packet should carry some simple way of telling receiver if开发者_运维百科 packet data is intact.
Now, UDP already has 2-byte checksum as part of header, which is optional, at least in IPv4 world. Alternative method is to have custom checksum as part of data section in each packet, and to verify it on receiver.
My question boils down to: is it better to rely on (optional) checksum in UDP packet header, or to make a custom checksum implementation as part of packet data section?
Perhaps the right answer depends on circumstances (as usual), so one circumstance here is that, even though code is written and developed in .NET on Windows, it might have to run under platform-independent Mono.NET, so eventual solution should be compatible with other platforms. I believe that custom checksum algorithm would be easily portable, but I'm not so sure about the first one.
Any thoughts?
Also, shouts about packet checksumming in general are welcome.
Do you need to count errors? If the UDP checksum is wrong then any router could just throw the packet away and you'd never get anything.
Of course, if there's an error in the header (such as destination IP address) you also wouldn't get it.
Also, the UDP checksum algorithm is completely standardized, although the function call to turn checksums on or off does vary by platform, although it usually involves setsockopt.
精彩评论