UDP可靠的数据服务实现

时间:2011-12-02 09:05:24

标签: timeout udp checksum packet

我正在尝试使用UDP实现简单的数据传输。我有一个校验和问题,给定一个包含数据的数据包,我该如何实现校验和?还知道如何实现超时,以便触发重传?感谢

2 个答案:

答案 0 :(得分:0)

为什么不尝试Reliable UDP,请参阅http://en.wikipedia.org/wiki/Reliable_User_Datagram_Protocol

它有一个标准。

答案 1 :(得分:0)

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!