测量ping-c+的分组丢失+; 我需要编写C++代码来测量ping的分组丢失-丢失的百分比。 我看到了IPHLPAPI库,其中包含大量关于RTT的统计信息,但没有数据包丢失

测量ping-c+的分组丢失+; 我需要编写C++代码来测量ping的分组丢失-丢失的百分比。 我看到了IPHLPAPI库,其中包含大量关于RTT的统计信息,但没有数据包丢失,c++,visual-studio-2010,ip,packet-loss,C++,Visual Studio 2010,Ip,Packet Loss,如果有人知道这件事,那就太好了 谢谢。您可以使用POCO库,它很小(比boost小很多),在网络方面比ACE更具可读性 以下是使用ICMPClient的ping程序的源代码 您可以在POCO_BASE/net/examples/Ping/src下找到它 #include "Poco/Util/Application.h" #include "Poco/Util/Option.h" #include "Poco/Util/OptionSet.h" #include "Poco/Util/HelpF

如果有人知道这件事,那就太好了


谢谢。

您可以使用POCO库,它很小(比boost小很多),在网络方面比ACE更具可读性

以下是使用ICMPClient的ping程序的源代码 您可以在POCO_BASE/net/examples/Ping/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();
}
#包括“Poco/Util/Application.h”
#包括“Poco/Util/Option.h”
#包括“Poco/Util/OptionSet.h”
#包括“Poco/Util/HelpFormatter.h”
#包括“Poco/Util/AbstractConfiguration.h”
#包括“Poco/Net/ICMPSocket.h”
#包括“Poco/Net/ICMPClient.h”
#包括“Poco/Net/IPAddress.h”
#包括“Poco/Net/ICMPEventArgs.h”
#包括“Poco/AutoPtr.h”
#包括“Poco/NumberParser.h”
#包括“Poco/Delegate.h”
#包括
#包括
使用Poco::Util::Application;
使用Poco::Util::Option;
使用Poco::Util::OptionSet;
使用Poco::Util::HelpFormatter;
使用Poco::Util::AbstractConfiguration;
使用Poco::Net::ICMPSocket;
使用Poco::Net::ICMPClient;
使用Poco::Net::IPAddress;
使用Poco::Net::ICMPEventArgs;
使用Poco::AutoPtr;
使用Poco::NumberParser;
使用Poco::Delegate;
类别:公开申请
///此示例演示了Poco::Net::ICMPClient与
//PoCO基金会C类事件功能。
///
///请尝试Ping--help(在Unix平台上)或Ping/help(在其他地方)以获取帮助
///更多信息。
{
公众:
Ping():
_请求帮助(错误),
_icmpClient(IP地址::IPv4),
_重复(4),
_目标(“本地主机”)
{
}
受保护的:
无效初始化(应用程序和自身)
{
loadConfiguration();//加载默认配置文件(如果存在)
应用程序::初始化(self);
_icmpClient.pingBegin+=委托(this,&Ping::onBegin);
_icmpClient.pingReply+=委托(this,&Ping::onReply);
_icmpClient.pingError+=委托(this,&Ping::onError);
_icmpClient.pingEnd+=委托(this,&Ping::oned);
}
作废取消初始化()
{
_icmpClient.pingBegin-=委托(this,&Ping::onBegin);
_icmpClient.pingReply-=委托(this,&Ping::onReply);
_icmpClient.pingError-=委托(this,&Ping::onError);
_icmpClient.pingEnd-=委托(this,&Ping::oned);
应用程序::取消初始化();
}
无效定义选项(选项开始和选项)
{
应用:定义选项(选项);
options.addOption(
选项(“帮助”、“h”、“显示命令行参数的帮助信息”)
。必填项(错误)
.可重复(错误));
options.addOption(
选项(“重复”、“r”、“定义重复次数”)
。必填项(错误)
.可重复(错误)
.论点(“重复”);
options.addOption(
选项(“目标”、“t”、“定义目标地址”)
。必填项(错误)
.可重复(错误)
.论点(“目标”);
}
void handleOption(常量std::string和name,常量std::string和value)
{
应用::handleOption(名称、值);
如果(名称=“帮助”)
_helprequest=true;
else if(名称=“重复”)
_重复=NumberParser::parse(值);
else if(名称==“目标”)
_目标=价值;
}
void displayHelp()
{
HelpFormatter HelpFormatter(选项());
setCommand(commandName());
setUsage(“选项”);
helpFormatter.setHeader(
“演示的功能的示例应用程序”
“Poco::Net::ICMPClient类与Poco::Events包功能结合使用。”);
格式(std::cout);
}
int main(常量std::vector和args)
{
如果(请求帮助)
显示帮助();
其他的
_icmpClient.ping(\u目标,\u重复);
返回应用程序::退出_确定;
}
void onBegin(const void*pSender、ICMPEventArgs和args)
{
std::ostringstream os;

os您可以使用POCO库,它很小(比boost小很多),在网络方面比ACE更具可读性

以下是使用ICMPClient的ping程序的源代码 您可以在POCO_BASE/net/examples/Ping/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();
}
#包括“Poco/Util/Application.h”
#包括“Poco/Util/Option.h”
#包括“Poco/Util/OptionSet.h”
#包括“Poco/Util/HelpFormatter.h”
#包括“Poco/Util/AbstractConfiguration.h”
#包括“Poco/Net/ICMPSocket.h”
#包括“Poco/Net/ICMPClient.h”
#包括“Poco/Net/IPAddress.h”
#包括“Poco/Net/ICMPEventArgs.h”
#包括“Poco/AutoPtr.h”
#包括“Poco/NumberParser.h”
#包括“Poco/Delegate.h”
#包括
#包括
使用Poco::Util::Application;
使用Poco::Util::Option;
使用Poco::Util::OptionSet;
使用Poco::Util::HelpFormatter;
使用Poco::Util::AbstractConfiguration;
使用Poco::Net::ICMPSocket;
使用Poco::Net::ICMPClient;
使用Poco::Net::IPAddress;
使用Poco::Net::ICMPEventArgs;
使用Poco::AutoPtr;
使用Poco::NumberParser;
使用Poco::Delegate;
类别:公开申请
///此示例演示了Poco::Net::ICMPClient与
//PoCO基金会C类事件功能。
///
///请尝试Ping--help(在Unix平台上)或Ping/help(在其他地方)以获取帮助
///更多信息。
{
公众:
Ping():
_请求帮助(错误),
_icmpClient(IP地址::IPv4),
_重复(4),
_目标(“本地主机”)
{
}
受保护的:
无效初始化(应用程序和自身)
{
loadConfiguration();//加载默认配置文件(如果存在)
应用程序::初始化(self);
_icmpClient.pingBegin+=委托(