问题
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!
来源:https://stackoverflow.com/questions/8353970/udp-reliable-data-service-implementation