我的教授给了我一个在C中实现选择性重复ARQ算法的任务,用于发送者和接收者之间的数据包事务。
有一个与发送方发送的每个数据包相关联的定时器,当发送该数据包时触发ON,根据该定时器确定需要发送哪个数据包副本。
但我不知道如何设置每个数据包的计时器。
请为它提出一些方法。
谢谢你提前!!
答案 0 :(得分:2)
答案 1 :(得分:2)
您也可以将Threads用于此目的,这非常简单,并且需要的代码行数较少。
您只需创建并定义此功能:
unsigned long CALLBACK packetTimer(void *pn){
//This is our Packet Timer
//It's going to run on a Tread
//If ack[thisPacketNumber] is not true
//We gonna check will check for packet time
//If it's reached to its limit we gonna send this packet again
//and reset the timer
//If ack[] becomes true
//break the while loop and this will end this tread
int pno = (int)pn;
std::clock_t start;
double duration;
start = std::clock();
while(1){
if(!ack[pno]){
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
if(duration > 0.5){
//This tells that we haven't received our ACk yet for this packet
//So send it again
printf("SendBuffer for Packet %d: %s", pno, packets[pno]->data);
//Resending packet again
send_unreliably(s,packets[pno]->data,(result->ai_addr));
//Reseting the timer
start = std::clock();
}
}else{break;}
}
}
在while循环中,您向接收者发送和接收数据包,您只需定义:
unsigned long tid;//This should be outside the while loop,
//Ideally in the beginning of main function
CreateThread(NULL,0,packetTimer,(void *)packetNumber,0,&tid);
这个实现适用于Windows,对于UNIX,我们需要使用pthread()
就是这样。 并且不要忘记添加所需的头文件,如:
#include <stdlib.h>
#include <cstdio>
#include <ctime>