SENDER [dummy packet 1]---ACK---[filetype packet]---ACK---[First file packet]---ACK---[Second file packet].... RECEIVER ----------------GOO---S/B-----------------GOO---S/B------------------GOO---S/B-- ------------------GOO...
SENDER --------------------------------------------------0x91---ACK---[odd data block]---ACK---SYN---S/B---ACK---[data block 1] RECEIVER GOO(over and over again until the transfer starts)----GOO---S/B---------------GOO---S/B---SYN---GOO---S/B----
RECEIVER: GOO SENDER: ACK RECEIVER: S/B SENDER: [filetype packet] RECEIVER: GOO SENDER: ACK RECEIVER: S/B SENDER: SYN RECEIVER: SYN SENDER: S/B RECEIVER: GOO SENDER: ACK RECEIVER: S/B SENDER: [dummy packet] RECEIVER: GOO SENDER: ACK Loop<--------------------------------- RECEIVER: S/B | SENDER: [file data packet] | RECEIVER: GOO | SENDER: ACK | -------------------------------------| RECEIVER: S/B SENDER: [final data packet] RECEIVER: GOO SENDER: ACK RECEIVER: S/B SENDER: SYN RECEIVER:SYN SENDER: S/B [filetype packet] Normal packet, one-byte payload, 1 for prg, 2 for seq, next block size of 4, block number of 0xffff [dummy packet] Normal packet, no payload, next block size as needed for the sent file, block number of 0x0000 [final packet] Normal packet, next block size of 0x00, and upper nybble of block number set to 0xff [file data packet, Normal packet] bytes 00, 01 : Additive checksum LO/HI bytes 02, 03 : CRC Checksum LO/HI byte 04 : Next block size bytes 05, 06 : block number LO/HI Checksums are calculated on all bytes from the Next Block Size forward to the end of the packet. Assuming TempPacket is your byte array of packet data and AdditiveChecksum and CLCChecksum are ushorts, checksum can be generated as follows (thanks to MagerValp for the original on this) public void Generate_Checksum() { AdditiveChecksum = 0; CLCChecksum = 0; for (int i = 4; i < TempPacket.Length; i++) { AdditiveChecksum += TempPacket[i]; CLCChecksum ^= TempPacket[i]; CLCChecksum = (ushort)((CLCChecksum << 1) | (CLCChecksum >> 15)); } } Validating received packets: Received packets should pass a checksum test, and be the proper length specified in the preceeding packet. Handshaking considerations: You may receive handshakes out-of-sync (KCA, CAK, etc...), your handshaking routines should be able to deal with this.
For receivers, dealing with bad packets. RECEIVER: BAD<---------------| SENDER: ACK | RECEIVER: S/B | SENDER: RESENDS SAME PACKET | RECEIVER: (BAD OR GOO)-------|