最初发布在这里,但发现偏离主题:https://serverfault.com/questions/617459/faster-processing-of-sendarp-function
我一直在为 Windows 开发网络扫描仪。我已经成功编写了代码,但问题是扫描未启动的主机需要花费太多时间。当我尝试扫描子网(1 到 255)时,花了半个多小时。我找不到控制时间限制的函数或控制 SendARP 函数超时的方法。
DestIp = inet_addr(strn.c_str()); //Setting Destination IPv4 dotted-decimal address into a proper address for the IN_ADDR structure.
SrcIp = inet_addr(SrcIpString);
memset(&MacAddr, 0xff, sizeof(MacAddr)); //Initializing MAC Address to ff-ff-ff-ff-ff-ff
dwRetVal = SendARP(DestIp, SrcIp, &MacAddr, &PhysAddrLen); //Sending ARP request to the destined IP Address
if (dwRetVal == NO_ERROR) {
bPhysAddr = (BYTE *)& MacAddr;
if (PhysAddrLen) {
std::cout << strn<<std::endl;
for (int i = 0; i < (int)PhysAddrLen; i++) {
if (i == ((int)PhysAddrLen - 1))
printf("%.2X\n", (int)bPhysAddr[i]);
else
printf("%.2X-", (int)bPhysAddr[i]);
}
}
}
最佳答案
您正在使用“IP Helper”库中的一个便捷函数。这不是以性能为导向的。
ServerFault 评论实际上正中要害:使用线程。与 <thread>
现在这很简单。就做 255 std::async
调用你的函数。当然,请确保所有 MacAddr
和 PhysAddrLen
引用不会失效。
关于c++ - 更快地处理 SendARP 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147865/