为什么在使用 POCO 库的 ICMP 客户端 ping 可达主机时出现 IOException?
Why getting IOException when pinging to rechable host using ICMPClient of POCO library?
当 ping 到可访问的主机时,我无法使用 POCO ping 功能获取 IOException。
下面是我的 ping 示例:
#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;
}
当 运行 此代码时出现以下异常:
terminate called after throwing an instance of 'Poco::IOException'
what(): I/O error
Aborted (core dumped)
当
时出现相同的异常
- 运行 Net/samples/Ping/src/Ping.cpp 没有任何参数的测试用例(在这种情况下,它应该 ping 到本地主机)或将目标指定为参数时
- 正在尝试 ping 到 SocketAddres。
- 尝试 ping 到可访问的主机而不是本地主机。
出现IOException异常的原因是什么?
ICMPClient 使用原始套接字。这意味着您必须 运行 您的可执行文件具有 root 权限。
看这里:Raw sockets need root priviliege
当 ping 到可访问的主机时,我无法使用 POCO ping 功能获取 IOException。 下面是我的 ping 示例:
#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;
}
当 运行 此代码时出现以下异常:
terminate called after throwing an instance of 'Poco::IOException'
what(): I/O error
Aborted (core dumped)
当
时出现相同的异常- 运行 Net/samples/Ping/src/Ping.cpp 没有任何参数的测试用例(在这种情况下,它应该 ping 到本地主机)或将目标指定为参数时
- 正在尝试 ping 到 SocketAddres。
- 尝试 ping 到可访问的主机而不是本地主机。
出现IOException异常的原因是什么?
ICMPClient 使用原始套接字。这意味着您必须 运行 您的可执行文件具有 root 权限。 看这里:Raw sockets need root priviliege