C++ 如何计算ICMP数据包的往返时间
我正在Qt5中编写一些C代码,以发送ICMP回送数据包来检测机器。我不知道如何计算往返时间 我的代码似乎工作不正常:C++ 如何计算ICMP数据包的往返时间,c++,c,qt,icmp,roundtrip,C++,C,Qt,Icmp,Roundtrip,我正在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(struc
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()
你想过使用吗?我得到了这个:“192.168.0.21”“----5.37912e+17毫秒”这是一个非常可疑的数字。它大约是10^48秒,这意味着你有一个64位的秒计数,这是搞砸了。请确保不要对64位值使用
htonl()
,因为htonl()
对32位值进行操作。我正在运行64位fedora 18,但我已经删除了htonl。谢谢我这样做了:foundItem.second=tr(“%1毫秒”).arg((quint32)((ts2.tv_sec-ts1.tv_sec)+(ts2.tv_usec-ts1.tv_usec)/1000000));但是仍然得到:“192.168.0.30”“---“3060823923 s”您可以使用函数.struct timezone tz;结构timeval ts1;结构timeval ts2;结构timeval ts3;b拷贝(&(pkt.icmp.data[8]),&ts1,sizeof(struct-timeval));gettimeofday(&ts2和&tz);timersub(&ts2和&ts1和&ts3);QHostAddress ha=QHostAddress(ntohl(pkt.ip.saddr));foundItem.first=ha.toString();foundItem.second=tr(“%1毫秒”).arg(ts3.tv_usec);“192.168.0.30”“----8316290828429423476 ms”为什么不使用您填写的ts
变量来代替网络数据?bcopy
的文档中说使用memmove
。(在这里您可以安全地使用memcpy
)
double round_trip_time = (ts2.tv_sec - ts1.tv_sec) +
1e-6 * (ts2.tv_usec - ts1.tv_usec);