UDP可靠的数据服务实现 [英] UDP reliable data service implementation

查看:112
本文介绍了UDP可靠的数据服务实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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!

这篇关于UDP可靠的数据服务实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆