I could not use POCO ping functionality as getting IOException when pinging to reachable host. Below is my ping example:
#include "Poco/Net/ICMPClient.h"
#include "Poco/Net/IPAddress.h"
#include "Poco/Net/ICMPEventArgs.h"
#include "Poco/Delegate.h"
#include <iostream>
using Poco::Net::ICMPClient;
using Poco::Net::IPAddress;
using Poco::Net::ICMPEventArgs;
class PingExample {
public:
PingExample():
_icmpClient(IPAddress::IPv4)
{
_icmpClient.pingBegin += Poco::delegate(this, &PingExample::onBegin);
_icmpClient.pingReply += Poco::delegate(this, &PingExample::onReply);
_icmpClient.pingError += Poco::delegate(this, &PingExample::onError);
_icmpClient.pingEnd += Poco::delegate(this, &PingExample::onEnd);
}
int start_ping(const std::string& host)
{
_icmpClient.ping(host);
}
void onBegin(const void* pSender, ICMPEventArgs& args)
{
std::cout << "Pinging " << args.hostName() << " [" << args.hostAddress() << "] with " << args.dataSize() << " bytes of data:"
<< std::endl << "---------------------------------------------" << std::endl;
}
void onReply(const void* pSender, ICMPEventArgs& args)
{
std::cout << "Reply from " << args.hostAddress()
<< " bytes=" << args.dataSize()
<< " time=" << args.replyTime() << "ms"
<< " TTL=" << args.ttl();
}
void onError(const void* pSender, ICMPEventArgs& args)
{
std::cout << args.error();
}
void onEnd(const void* pSender, ICMPEventArgs& args)
{
std::cout << 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 << "------------------------------------------";
}
private:
ICMPClient _icmpClient;
};
int main(int argc, char** argv)
{
PingExample p;
p.start_ping("localhost");
while (true);
return 0;
}
Getting the following exception when running this code:
terminate called after throwing an instance of 'Poco::IOException'
what(): I/O error
Aborted (core dumped)
The same exception is getting when
What is the reason of getting IOException exception?
ICMPClient uses raw sockets. That means that you have to run your executable with root permission. See here: Raw sockets need root priviliege