0
A
回答
0
当涉及到网络时,您可以使用POCO库,它比ACE更小(ALOT小于boost),并且比ACE更具可读性。
下面是一个使用ICMPClient 一个ping程序的源代码,你会发现它在POCO_BASE/NET /例子/平/ src目录
#include "Poco/Util/Application.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/Net/ICMPSocket.h"
#include "Poco/Net/ICMPClient.h"
#include "Poco/Net/IPAddress.h"
#include "Poco/Net/ICMPEventArgs.h"
#include "Poco/AutoPtr.h"
#include "Poco/NumberParser.h"
#include "Poco/Delegate.h"
#include <iostream>
#include <sstream>
using Poco::Util::Application;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
using Poco::Util::AbstractConfiguration;
using Poco::Net::ICMPSocket;
using Poco::Net::ICMPClient;
using Poco::Net::IPAddress;
using Poco::Net::ICMPEventArgs;
using Poco::AutoPtr;
using Poco::NumberParser;
using Poco::Delegate;
class Ping: public Application
/// This sample demonstrates the Poco::Net::ICMPClient in conjunction with
/// Poco Foundation C#-like events functionality.
///
/// Try Ping --help (on Unix platforms) or Ping /help (elsewhere) for
/// more information.
{
public:
Ping():
_helpRequested(false),
_icmpClient(IPAddress::IPv4),
_repetitions(4),
_target("localhost")
{
}
protected:
void initialize(Application& self)
{
loadConfiguration(); // load default configuration files, if present
Application::initialize(self);
_icmpClient.pingBegin += Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin);
_icmpClient.pingReply += Delegate<Ping, ICMPEventArgs>(this, &Ping::onReply);
_icmpClient.pingError += Delegate<Ping, ICMPEventArgs>(this, &Ping::onError);
_icmpClient.pingEnd += Delegate<Ping, ICMPEventArgs>(this, &Ping::onEnd);
}
void uninitialize()
{
_icmpClient.pingBegin -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin);
_icmpClient.pingReply -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onReply);
_icmpClient.pingError -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onError);
_icmpClient.pingEnd -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onEnd);
Application::uninitialize();
}
void defineOptions(OptionSet& options)
{
Application::defineOptions(options);
options.addOption(
Option("help", "h", "display help information on command line arguments")
.required(false)
.repeatable(false));
options.addOption(
Option("repetitions", "r", "define the number of repetitions")
.required(false)
.repeatable(false)
.argument("repetitions"));
options.addOption(
Option("target", "t", "define the target address")
.required(false)
.repeatable(false)
.argument("target"));
}
void handleOption(const std::string& name, const std::string& value)
{
Application::handleOption(name, value);
if (name == "help")
_helpRequested = true;
else if (name == "repetitions")
_repetitions = NumberParser::parse(value);
else if (name == "target")
_target = value;
}
void displayHelp()
{
HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader(
"A sample application that demonstrates the functionality of the "
"Poco::Net::ICMPClient class in conjunction with Poco::Events package functionality.");
helpFormatter.format(std::cout);
}
int main(const std::vector<std::string>& args)
{
if (_helpRequested)
displayHelp();
else
_icmpClient.ping(_target, _repetitions);
return Application::EXIT_OK;
}
void onBegin(const void* pSender, ICMPEventArgs& args)
{
std::ostringstream os;
os << "Pinging " << args.hostName() << " [" << args.hostAddress() << "] with " << args.dataSize() << " bytes of data:"
<< std::endl << "---------------------------------------------" << std::endl;
logger().information(os.str());
}
void onReply(const void* pSender, ICMPEventArgs& args)
{
std::ostringstream os;
os << "Reply from " << args.hostAddress()
<< " bytes=" << args.dataSize()
<< " time=" << args.replyTime() << "ms"
<< " TTL=" << args.ttl();
logger().information(os.str());
}
void onError(const void* pSender, ICMPEventArgs& args)
{
std::ostringstream os;
os << args.error();
logger().information(os.str());
}
void onEnd(const void* pSender, ICMPEventArgs& args)
{
std::ostringstream os;
os << std::endl << "--- Ping statistics for " << args.hostName() << " ---"
<< std::endl << "Packets: Sent=" << args.sent() << ", Received=" << args.received()
<< " Lost=" << args.repetitions() - args.received() << " (" << 100.0 - args.percent() << "% loss),"
<< std::endl << "Approximate round trip times in milliseconds: " << std::endl
<< "Minimum=" << args.minRTT() << "ms, Maximum=" << args.maxRTT()
<< "ms, Average=" << args.avgRTT() << "ms"
<< std::endl << "------------------------------------------";
logger().information(os.str());
}
private:
bool _helpRequested;
ICMPClient _icmpClient;
int _repetitions;
std::string _target;
};
int main(int argc, char** argv)
{
AutoPtr<Ping> pApp = new Ping;
try
{
pApp->init(argc, argv);
}
catch (Poco::Exception& exc)
{
pApp->logger().log(exc);
return Application::EXIT_CONFIG;
}
return pApp->run();
}
您可以删除记录器/帮助的东西,使源位使用OnReply/OnError/OnEnd来计算丢包率
您发送4个数据包.... 3个数据包返回(OnReply)数据包丢失= 1-3/4 = 25%。
+0
谢谢!我会尝试。 哪里是POCO_BASE库?它是否已经在我的电脑上?我需要下载它吗? – user1673206
相关问题
- 1. 使用Java测量数据包丢失
- 2. 数据包丢失测试
- 3. Python丢失包
- 4. 包org.apache.harmony丢失?
- 5. javax.realtime包丢失
- 6. 丢失包
- 7. QT测试 - 变量/对象丢失值
- 8. 检测TCP丢失?
- 9. AWS RHEL7丢失包
- 10. Pellet-2.3包丢失?
- 11. 检测丢失的'commitAnimations'
- 12. ipython中丢失的包imutils
- 13. Glassfish的 - 丢失包= org.apache.commons.collections
- 14. javascript丢包测试
- 15. C++丢失变量
- 16. 如何使用套接字编程测量Android中的数据包丢失?
- 17. 测量各个TCP连接丢弃的数据包数量
- 18. SharpPCap丢失数据包
- 19. 吴 - 包括丢失范围
- 20. Android 1.6丢失数据包
- 21. Android udp数据包丢失
- 22. 最后一包丢失gnuradio
- 23. Android UDP数据包丢失
- 24. 50%udp数据包丢失
- 25. Delphi 2006丢失组件包
- 26. C#TcpClient丢失数据包
- 27. HID数据包丢失
- 28. NuGet包文件丢失
- 29. MSDeploy包丢失文件
- 30. Haskell包丢失c库
你的问题需要一个更明确的目标,你实际上在问什么。到目前为止,你做了什么,你确切地知道什么? –
o.k. 我有发送ping到特定IP地址的代码。在ICMP响应中,存在具有所有参数的结构,例如RTT,DataSize ...但我还需要该ping的丢包。 我希望它更清楚一点。 – user1673206
我之前做过这样的事情,我只是在标准ping之上使用了一个小脚本,它在接收数据包的时间内计算了“丢失”数据包的数量。但是,如果您编写自己的代码,则只需发送一个数据包,然后等待回复。如果在X秒内没有回复,它就是一个“丢失”的数据包。 (这里X可能不是整数) –