I'm trying to implement a simple data transfer using UDP. I have a problem for the checksum, given a packet containing the data, how should I implement the checksum? also any idea how to implement the timeouts so it will trigger the retransmission ? Thanks
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Why not try Reliable UDP, see http://en.wikipedia.org/wiki/Reliable_User_Datagram_Protocol
It has a standard.
回答2:
here's one approach for the internet checksum
unsigned short checkSum() {
unsigned long sum = 0;
int i;
for(i=0; i < your packet length ; i++) {
sum += (your packet data[i] & 0xFFFF);
}
while (sum >> 16) {
sum = (sum & 0xFFFF) + (sum >> 16);
}
sum = ~sum;
return ((unsigned short) sum);
}
for the retransmission, you can set alarm to trigger timeout
when packet is loss. you can do something using
signal (SIGALRM, timeout function);
Hope it helps!