Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 没有用户登录时,系统进程将失去网络连接_C++_Windows_Http_Networking - Fatal编程技术网

C++ 没有用户登录时,系统进程将失去网络连接

C++ 没有用户登录时,系统进程将失去网络连接,c++,windows,http,networking,C++,Windows,Http,Networking,我试图在windows 10计算机上运行一个程序,该程序通过系统服务启动,以便在没有用户登录时继续执行。该程序是用C++编写的,使用C++ LIbCURL包装器在端口80上查询HTTP Web服务器。 在一段看似随机的时间之后,没有用户登录(计算机在登录屏幕上),通常在1到15分钟之间,有时更长,http请求开始一致地返回错误代码0,这显然不是有效的http错误代码,但cpr返回的是状态代码。 在用户再次登录之前,请求不会返回到成功状态,在此情况下,它们将无限期地按预期运行(我已经进行了24小时

我试图在windows 10计算机上运行一个程序,该程序通过系统服务启动,以便在没有用户登录时继续执行。该程序是用C++编写的,使用C++ LIbCURL包装器在端口80上查询HTTP Web服务器。 在一段看似随机的时间之后,没有用户登录(计算机在登录屏幕上),通常在1到15分钟之间,有时更长,http请求开始一致地返回错误代码0,这显然不是有效的http错误代码,但cpr返回的是状态代码。 在用户再次登录之前,请求不会返回到成功状态,在此情况下,它们将无限期地按预期运行(我已经进行了24小时测试)。 机器已禁用所有形式的睡眠/休眠

检查事件查看器时,系统日志中只有一种事件类型属于错误开始发生的区域

Log Name: System
Source: Schannel
Event ID: 36884
Level: Error
User: System
Message: The certificate received from the remote server does not contain the expected name. It is therefore not possible to determine whether we are connecting to the correct server. The server name we were expecting is client.wns.windows.com. The TLS connection request has failed. The attached data contains the server certificate.
该错误似乎无关,特别是由于服务器名称,以及我的程序http请求不通过TLS,但在请求开始失败后会定期报告,并在用户重新登录时停止,从而允许http请求成功。 还有一些看似零星的Service Control Manager事件表明后台智能传输服务的启动类型已从自动启动更改为按需启动,或从按需启动更改为自动启动,但是,事件总是被报告的,并且似乎并不特定于我的程序无法发送请求的时间段

在此期间,程序和启动程序的服务都能正常运行,因为它们可以将错误报告到日志文件中,而不会出现任何问题。以防万一,该程序还使用win32 api的PowerCreateRequest和PowerSetRequest函数创建SystemRequired和ExecutionRequired电源请求

我找不到任何逻辑上的理由让这些请求在几分钟后“不起作用”,这非常令人愤怒。请在评论中询问任何其他信息

编辑:流程代码:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <cpr/cpr.h>
 
#pragma comment(lib, "PowrProf.lib")
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    REASON_CONTEXT context;
    context.Version = POWER_REQUEST_CONTEXT_VERSION;
    context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
    context.Reason.SimpleReasonString = std::wstring(L"idle").data();
    HANDLE req = PowerCreateRequest(&context);
    if (req != INVALID_HANDLE_VALUE)
    {
        PowerSetRequest(req, PowerRequestSystemRequired);
        PowerSetRequest(req, PowerRequestAwayModeRequired);
        PowerSetRequest(req, PowerRequestExecutionRequired);
    }
   
    while (true)
    {
        Sleep(10000);
        auto r = cpr::Post(
          cpr::Url{ "http://<IP>:80/update" },
          cpr::Header{ {"Authorization", "Basic " + "<BASE64_USER:PASS_PAIR>"}, {"Content-Type", "application/json"} },
          cpr::Body{ "{'data': 'test'}" },
          cpr::Timeout{ 5000 }
        );
    }
   
    if (req != INVALID_HANDLE_VALUE)
    {
        PowerClearRequest(req, PowerRequestSystemRequired);
        PowerClearRequest(req, PowerRequestAwayModeRequired);
        PowerClearRequest(req, PowerRequestExecutionRequired);
        CloseHandle(req);
    }
    return 0;
}
#定义WIN32_LEAN_和_MEAN
#包括
#包括
#pragma注释(lib,“powrpof.lib”)
int-WINAPI WinMain(HINSTANCE-HINSTANCE、HINSTANCE-hPrevInstance、LPSTR-lpCmdLine、int-nShowCmd)
{
理性与语境;
context.Version=POWER\u REQUEST\u context\u Version;
context.Flags=POWER\u REQUEST\u context\u SIMPLE\u STRING;
context.Reason.SimpleReasonString=std::wstring(L“idle”).data();
HANDLE req=PowerCreateRequest(&context);
if(req!=无效的\u句柄\u值)
{
PowerSetRequest(需要,PowerRequestSystemRequired);
PowerSetRequest(请求、PowerRequestAwayRequired);
PowerSetRequest(请求、PowerRequestExecutionRequired);
}
while(true)
{
睡眠(10000);
自动r=cpr::Post(
cpr::Url{“http://:80/update”},
cpr::头{{“授权”、“基本”+“”}、{“内容类型”、“应用程序/json”},
cpr::Body{{{'data':'test'}},
cpr::超时{5000}
);
}
if(req!=无效的\u句柄\u值)
{
PowerClearRequest(req,PowerRequestSystemRequired);
PowerClearRequest(请求、PowerRequestAwayRequired);
PowerClearRequest(需要请求、PowerRequestExecutionRequired);
闭合手柄(req);
}
返回0;
}

您能否在cpr中设置与CURLOPT_VERBOSE等效的值?这可能会给你一些线索,“通过系统服务启动”——为什么不将其作为自己的系统服务来实现呢?您的服务是否为网络/TCP服务设置了依赖项?“http请求开始一致地返回错误代码0”-请您的问题显示您的实际代码。@RemyLebeau代码处于单独进程中的原因是,它只是使开发过程中更易于运行和调试,代码简单,功能正常,但我要补充一点it@Botje它似乎曾经有一个详细的选项,但它被删除了,所以我不知道如何做that@Botje我认为只使用一个具有详细功能的不同库会更容易,所以我将在下一步尝试,因为显然我没有为任何人提供足够的信息来提供有效的解决方案,一个有效的http错误代码将是朝着正确方向迈出的一步