一个丢包率计算程序

 假设数据包不重复,能够容忍一定程度的失序。一个适用于UDP的传输场景,周期性统计数据包的丢包率。若上个周期统计的丢失包在本周期中到来,不对这些数据包进行统计。
其中,capacity表示一轮中数据包的个数,比如,数据包序号可以取值0-29,capacity就是30;若数据包序号可以取值0-65535,capacity就是65536.

/*
*author zsy
*stat packets loss in network
*handle out of order packets
*/
#include<iostream>
#include<stdio.h>
#include<stdbool.h>
class PacketLoss
{
public:
    PacketLoss(uint64_t start_seq,uint64_t capacity);
    ~PacketLoss(){}
    void IncomingPacket(uint64_t seq);
    float GetLossRate();
    void Reset();
private:
    uint64_t min_seq_;
    uint64_t max_seq_;
    uint64_t total_packet_;
    float loss_rate_;
    uint64_t capacity_;
    bool first_;//
};
PacketLoss::PacketLoss(uint64_t start_seq,uint64_t capacity)
{
    min_seq_=start_seq;
    max_seq_=start_seq;
    total_packet_=0;
    loss_rate_=0.0;
    capacity_=capacity;
    first_=true;
}
void PacketLoss::IncomingPacket(uint64_t seq)
{
    if(first_&&seq==0)
    {
        total_packet_++;//packet seq 0;
    }
    if(seq>max_seq_)
    {
        if(seq-max_seq_>capacity_/2)
        {

        }else{
            max_seq_=seq;
            printf("mark%d\n",seq);
        }
    }
    if(seq<max_seq_)
    {
        if(max_seq_-seq>capacity_/2)
        {
            max_seq_=seq;
        }
    }
    if(seq>min_seq_)
    {
        if(seq-min_seq_>capacity_/2)
        {
            //receive packets belongs to last round;
        }else{
            total_packet_++;
        }
    }
    if(seq<min_seq_)
    {
        if(min_seq_-seq>capacity_/2)
        {
            total_packet_++;
        }
    }
}
float PacketLoss::GetLossRate()
{
    float range=0.0;
    if(max_seq_>min_seq_)
    {
        printf("%d\n",max_seq_-min_seq_);
        printf("fuck %llu,%llu,%llu\n",min_seq_,max_seq_,total_packet_);
        range=max_seq_-min_seq_;
        if(first_)
        {
            loss_rate_=(range+1-total_packet_)/(range+1);
        }else
        {
            loss_rate_=(range-total_packet_)/range;
        }
    }
    if((max_seq_<min_seq_)&&(min_seq_-max_seq_)>capacity_/2)
    {
        printf("%llu,%llu,%llu\n",min_seq_,max_seq_,total_packet_);
        range=capacity_-min_seq_+max_seq_;
        loss_rate_=(range-total_packet_)/range;
    }
    return loss_rate_;
}
void PacketLoss::Reset()
{
    total_packet_=0;
    min_seq_=max_seq_;
    first_=false;

}
int main()
{
uint64_t array[10]={0,1,2,5,6,7,3,8,9,10};
PacketLoss packetloss(0,30);
int i=0;
for(i=0;i<10;i++)
{
    packetloss.IncomingPacket(array[i]);//0 as marker
}
printf("loss rate %f\n",packetloss.GetLossRate());
packetloss.Reset();
uint64_t array1[10]={11,13,12,15,19,17,18,16,20,22};
for(i=0;i<10;i++)
{
    packetloss.IncomingPacket(array1[i]);//0 as marker
}
printf("loss rate %f\n",packetloss.GetLossRate());
uint64_t array2[10]={14,21,23,24,29,25,26,0,1,2};
packetloss.Reset();
for(i=0;i<10;i++)
{
    packetloss.IncomingPacket(array2[i]);//0 as marker
}
printf("loss rate %f\n",packetloss.GetLossRate());
}
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您提供一些有关使用Qt编写TCP计算包率的示例代码。首先,您需要创建一个TCP套接字并与远程服务器建立连接。然后,您可以使用QT的信号和槽机制来检测失的数据包,并计算包率。以下是示例代码: ``` #include <QTcpSocket> #include <QObject> class MyTcpSocket : public QTcpSocket { Q_OBJECT public: MyTcpSocket(QObject *parent = nullptr) : QTcpSocket(parent) { connect(this, SIGNAL(readyRead()), this, SLOT(readData())); connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError))); m_totalPackets = 0; m_lostPackets = 0; } public slots: void readData() { // Read the incoming data QByteArray data = readAll(); // Increment the packet count ++m_totalPackets; // Check if packet lost if (m_totalPackets != data.toUInt()) { ++m_lostPackets; } // Calculate and display packet loss percentage double lossPercentage = (double) m_lostPackets / m_totalPackets * 100; qDebug() << "Packet loss percentage: " << QString::number(lossPercentage, 'f', 2) << "%"; } void displayError(QAbstractSocket::SocketError socketError) { qDebug() << "Socket error: " << socketError; } private: quint64 m_totalPackets; quint64 m_lostPackets; }; ``` 在上面的示例中,我们创建了一个`MyTcpSocket`类,该类继承自`QTcpSocket`类。我们使用`connect`函数将`readyRead`和`error`信号连接到相应的槽函数中。在`readData`函数中,我们检查接收到的数据是否与之前发送的数据相同,并相应地递增失的数据包的数量。最后,我们计算包率并显示其百分比。在`displayError`函数中,我们仅仅输出错误信息。 当您使用此示例程序时,请确保将`MyTcpSocket`类实例化并连接到远程服务器(使用`connectToHost`函数)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值