简体   繁体   English

“访问冲突”,“分配错误”:POCO NTP客户端请求C ++

[英]'Access Violation', 'bad allocation': POCO NTPClient request C++

I wanted to make a simple program which does the following: 'Get Atomic Time from Internet Clock'. 我想做一个简单的程序,该程序执行以下操作:“从Internet Clock获取原子时间”。 I already did another program that uses FTP, and I did this with the Poco::Net library. 我已经做过另一个使用FTP的程序,并且是通过Poco :: Net库完成的。

I tried using NTPClient, since I read that is the Network Time Protocol. 我尝试使用NTPClient,因为我读到的是网络时间协议。 In more detail, here is the information I worked with: 更详细地,这是我处理过的信息:

'The NIST servers listen for a NTP request on port 123, and respond by sending a udp/ip data packet in the NTP format. “ NIST服务器在端口123上侦听NTP请求,并通过以NTP格式发送udp / ip数据包进行响应。 The data packet includes a 64-bit timestamp containing the time in UTC seconds since January 1, 1900 with a resolution of 200 ps.' 数据包包括一个64位时间戳,其中包含自1900年1月1日以来以UTC秒为单位的时间,分辨率为200 ps。

My source is this website: nist 我的来源是这个网站: nist

Also, I tried various servers from here, which had the status 'all services available': servers 另外,我从这里尝试了各种服务器,这些服务器的状态为“所有服务可用”: 服务器

Here is a small example which crashes and/or throws an exception. 这是一个崩溃和/或引发异常的小示例。 It is an exception std::bad_alloc and sometimes it crashes with 'access violation' (see below). 这是一个std :: bad_alloc异常,有时会因“访问冲突”而崩溃(请参阅下文)。 I am compiling with VC++12 on Windows 8 我正在Windows 8上使用VC ++ 12进行编译

The docs I worked with: NTPClient 我合作过的文档: NTPClient

#include <iostream>
#include <stdexcept>

#include <Poco/Net/NTPClient.h>

using namespace Poco::Net;

int main()
{
    try {
        NTPClient client { IPAddress::Family::IPv4 };

             // this is where the bad_alloc comes from:
        client.request("129.6.15.30:123");  // or any other server

    }
    catch (std::exception& e) {
        std::cerr << e.message() << '\n';
    }
}

I don't know why this code is causing the bad_alloc Exception or access violation, so I hope someone can point out what I am doing wrong. 我不知道为什么这段代码会导致bad_alloc异常或访问冲突,所以我希望有人指出我做错了什么。 I doubt it is a bug in the Library, I am probally just using it wrong (maybe it is?). 我怀疑这是库中的错误,我可能是错误地使用了它(也许是吗?)。

Update I also sometimes get '...0xC0000005: Access violation reading location 0x00E22CA9.' 更新我有时也会得到“ ... 0xC0000005:访问冲突读取位置0x00E22CA9”。 (the second memory location varies). (第二个内存位置有所不同)。 However, depending on the IP-Address, the bad_alloc error still is caught sometimes (no crash). 但是,根据IP地址,有时仍会捕获bad_alloc错误(不会崩溃)。 So for some IPs it crashes with access violation, with some it throws bad_alloc exception and terminates. 因此,对于某些IP,它会因访问冲突而崩溃,而对于某些IP,它会抛出bad_alloc异常并终止。 Don't know if that piece of information might be helpful. 不知道那条信息是否会有所帮助。 Also, they error occurs, even if I feed complete garbage as IP-Address eg "foobar". 此外,即使我将完整的垃圾作为IP地址(例如“ foobar”)提供,它们也会发生错误。

Until someone has an idea, I will examine the sources and try to find something (though that is hard with my level of experience). 直到有人有想法之前,我都会检查源代码并尝试查找某些内容(尽管以我的经验水平很难)。

Update2: After setting the libs to debug libs (I wanted more information, wanted to add the PocoNetd.pdb (I think it allows VS to debug pocos code too?) it doesn't happen anymore... weird, programming is weird... Update2:在将库设置为调试库之后(我想了解更多信息,想添加PocoNetd.pdb(我认为它也允许VS调试pocos代码吗?)),这种情况不再发生了……怪异,编程很怪异。 ..

I was not able to find a problem with POCO here. 我在这里找不到POCO的问题。 But there is a problem with posted code - there is no such thing as std::exception::message() so the code does not compile. 但是发布的代码存在问题-没有std :: exception :: message()之类的东西,因此代码无法编译。 Changing the message() to what() and executing, one gets "Host not found" exception description. 将message()更改为what()并执行,将获得“找不到主机”异常描述。 Changing NTP server to "pool.ntp.org", it executes fine. 将NTP服务器更改为“ pool.ntp.org”,它可以正常执行。 Here's the code that runs well for me on Windows 8, VS2013, 64-bit build: 这是在Windows 8,VS2013、64位版本上对我来说运行良好的代码:

#include <iostream>
#include <stdexcept>
#include <Poco/Net/NTPClient.h>
using namespace Poco::Net;

int main()
{
    try {
        NTPClient client{ IPAddress::Family::IPv4 };
        client.request("pool.ntp.org");
    }
    catch (std::exception& e) {
        std::cerr << e.what() << '\n';
    }
}

EDIT: the reason for "Host not found" exception in the original code is because the port is embedded in the string passed to the request() call but the implementation hard codes the port and expects ip address or host name only. 编辑:原始代码中“未找到主机”异常的原因是因为端口嵌入在传递给request()调用的字符串中,但是实现对端口进行硬编码 ,并且仅期望IP地址或主机名。

EDIT2: Just an advice: If your goal is to run debug binaries, then you may consider your problem solved. EDIT2:只是一个建议:如果您的目标是运行调试二进制文件,则可以考虑解决问题。 Otherwise, you should be able to build and run a release build of your application with release versions of POCO libraries. 否则,您应该能够使用POCO库的发行版来构建和运行应用程序的发行版。 For an example how to do that, look into any of the Poco::Net sample VS projects settings. 有关如何执行此操作的示例,请查看任何Poco :: Net示例VS项目设置。

After setting the libs to debug libs (I wanted more information, wanted to add the PocoNetd.pdb (I think it allows VS to debug pocos code too?) it doesn't happen anymore... weird, programming is weird... I am also able so successfully compile and run a realease version. 在将库设置为调试库之后(我想了解更多信息,想添加PocoNetd.pdb(我认为它也允许VS调试pocos代码吗?))它不再发生了……怪异,编程很怪异……我也能够如此成功地编译并运行发布版本。

So in a nutshell: I messed up my settings, and POCO yet has worked flawlessly for me. 简而言之:我弄乱了设置,而POCO却为我完美地工作了。 It's interesting, what kind of errors you get by messing up Debug/Release. 有趣的是,搞错了Debug / Release会导致什么样的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM