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. 丟失包
- 5. javax.realtime包丟失
- 6. 包org.apache.harmony丟失?
- 7. QT測試 - 變量/對象丟失值
- 8. 檢測TCP丟失?
- 9. Pellet-2.3包丟失?
- 10. AWS RHEL7丟失包
- 11. 檢測丟失的'commitAnimations'
- 12. Glassfish的 - 丟失包= org.apache.commons.collections
- 13. ipython中丟失的包imutils
- 14. javascript丟包測試
- 15. C++丟失變量
- 16. 如何使用套接字編程測量Android中的數據包丟失?
- 17. 測量各個TCP連接丟棄的數據包數量
- 18. 包丟失在Maven構建
- 19. NuGet包文件丟失
- 20. MSDeploy包丟失文件
- 21. Delphi 2006丟失組件包
- 22. Android 1.6丟失數據包
- 23. Haskell包丟失c庫
- 24. udp數據包丟失
- 25. Android udp數據包丟失
- 26. C#TcpClient丟失數據包
- 27. 最後一包丟失gnuradio
- 28. SharpPCap丟失數據包
- 29. 吳 - 包括丟失範圍
- 30. 50%udp數據包丟失
你的問題需要一個更明確的目標,你實際上在問什麼。到目前爲止,你做了什麼,你確切地知道什麼? –
o.k. 我有發送ping到特定IP地址的代碼。在ICMP響應中,存在具有所有參數的結構,例如RTT,DataSize ...但我還需要該ping的丟包。 我希望它更清楚一點。 – user1673206
我之前做過這樣的事情,我只是在標準ping之上使用了一個小腳本,它在接收數據包的時間內計算了「丟失」數據包的數量。但是,如果您編寫自己的代碼,則只需發送一個數據包,然後等待回覆。如果在X秒內沒有回覆,它就是一個「丟失」的數據包。 (這裏X可能不是整數) –