我的教授给了我一个任务,让我用C语言实现选择性重复ARQ算法,用于发送方和接收方之间的数据包处理。存在与要在发送器处发送的每个分组相关联的定时器,该定时器在该分组被发送时被触发,根据该定时器来确定需要发送哪个分组副本。
但是我不知道如何设置每个数据包的计时器。请推荐一些解决方法。
感谢是前进!!
发布于 2015-05-24 21:57:42
发布于 2016-10-14 09:32:08
您也可以使用Thread来实现此目的,这非常简单,并且需要的代码行更少。
您只需要创建和定义此函数:
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>https://stackoverflow.com/questions/30420971
复制相似问题