UDP reliable data service implementation

2019-08-20 17:57发布

问题:

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!