如何计算ICMP数据包的往返时间
How to calculate round trip time for an ICMP packet
我正在Qt5中编写一些C代码,以发送ICMP回显数据包来检测机器。我不知道如何计算往返时间。
我的代码似乎无法正常工作:
Before sending:
struct timezone tz;
struct timeval ts;
gettimeofday( &ts, &tz );
ts.tv_sec = ts.tv_sec;
ts.tv_usec = ts.tv_usec;
bcopy(&ts, &(pkt.icmp.data[0]), sizeof(struct timeval)); // target host will modify this
bcopy(&ts, &(pkt.icmp.data[8]), sizeof(struct timeval));
After receiving:
struct timezone tz;
struct timeval ts1;
struct timeval ts2;
bcopy(&(pkt.icmp.data[8]), &ts1, sizeof(struct timeval));
gettimeofday( &ts2, &tz );
round trip time = (ts2.tv_sec - ts1.tv_sec) +
1e-6 * (ts2.tv_usec - ts1.tv_usec);
有什么问题吗?
感谢
编辑:这是接收功能:
void CPingReceiver::dataProcess(struct icmp_packet pkt)
{
struct timezone tz;
struct timeval ts1;
struct timeval ts2;
bcopy(&(pkt.icmp.data[8]), &ts1, sizeof(struct timeval));
gettimeofday( &ts2, &tz );
QHostAddress ha = QHostAddress(ntohl(pkt.ip.saddr));
foundItem.first = ha.toString();// (ts2.tv_sec * 1000 + ts2.tv_usec / 1000) - (ts1.tv_sec * 1000 + ts1.tv_usec / 1000)
foundItem.second = tr("%1 ms").arg(((ts2.tv_sec - ts1.tv_sec) +
(ts2.tv_usec - ts1.tv_usec) / 1000000));
emit sendToListener(foundItem);
// qDebug() << addr << endl;
// now send the data to ARP Worker Singleton
// PING results will send its data to ARP Worker Singleton as well
// same for hostname, vendor and netbios, open ports
}
这里是发送功能:
/************************************************************************
* Build ICMP Header
************************************************************************/
pkt.icmp.type = ICMP_ECHO; // icmp echo */
pkt.icmp.code = 0; // only valid value for echo or echo reply */
pkt.icmp.checksum = 0;
pkt.icmp.identifier = ICMP_IDENTIFIER; // the id we'll be using to distinguish our data from other icmp packets */
pkt.icmp.sequence = 1; // Start from 0
struct timezone tz;
struct timeval ts;
gettimeofday( &ts, &tz );
bzero(pkt.icmp.data, ICMP_MTU);
bcopy(&ts, &(pkt.icmp.data[0]), sizeof(struct timeval));
bcopy(&ts, &(pkt.icmp.data[8]), sizeof(struct timeval));
pkt.icmp.checksum = calcsum((quint16 *)(&pkt.icmp), sizeof(pkt.icmp));
这就是我得到的:
"192.168.0.21" "----" "F0:7D:68:04:49:86" // ARP reply
"192.168.0.28" "----" "00:19:5B:0D:30:85" // ARP reply
"192.168.0.30" "----" "00:04:20:2C:83:34" // ARP reply
"-------------PING reply-----------------" "192.168.0.21" "----" "-8316290828429 ms"
"192.168.0.26" "----" "74:44:01:D3:07:E0" // ARP reply
"-------------PING reply---------------" "192.168.0.26" "----" "-8316290828429 ms"
"-------------PING reply---------------" "192.168.0.30" "----" "-8316290828429 ms"
"192.168.0.23" "----" "C8:60:00:1A:B0:BC" // ARP reply
"-------------PING reply---------------" "192.168.0.23" "----" "-8316290828429 ms"
gettimeofday()
函数以本机字节顺序提供时间值,而不一定以网络字节顺序提供。不要调用ntohl()
。
double round_trip_time = (ts2.tv_sec - ts1.tv_sec) +
1e-6 * (ts2.tv_usec - ts1.tv_usec);
您想过使用QElapedTimer吗?
相关文章:
- boost::asio UDP 广播客户端仅接收"fast"数据包
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 发送固定大小的 UDP 数据包
- pcap_handler回调仅在使用 NPCAP v0.9991 时包含空数据包
- 在 c++ 中解析数据包数据的最佳方法是什么?
- 接受函数在发送数据包时等待
- 如何在 omnet++ 中发送自定义数据包?
- 数据包访问实践
- 在C++中创建一个简单的数据包路由器,如何跟踪"客户端"?
- 德拉吉诺 LG01-S 收到异常数据包并停止工作
- 将数据包从C++服务器发送到NodeJs服务器时出现MessagePack解码错误
- 使用C++将UDP数据包存储在Structure中
- FFmpeg av_read_frame从音频流返回数据包
- 为什么操作系统正在更改我的数据包的指定传出端口
- 是否可以将多个结构作为一个数据包存储在一个函数中,然后传递给其他函数并在那里提取?
- recvfrom 只收到几个数据包,之后它进入等待状态
- 从 NS3 获取简单的指标,如吞吐量与时间、数据包丢失与时间的关系
- 以 ns3 为单位计算数据包接收时间
- 如何计算ICMP数据包的往返时间
- 如何在OMNeT++中存储不同时间戳的数据包