如何使用 c++ 对 php 页面进行 ping 操作

How to ping a php page using c++?

本文关键字:ping 操作 何使用 c++ php      更新时间:2023-10-16

我希望能够向我的网站发出信号,表明我的 c++ 应用程序当前正在运行。到目前为止,我所做的是使用 libcurl 联系我网站上的一个带有一些 get 参数的 PHP 文件。
这是C++代码:

curl_global_init(CURL_GLOBAL_ALL);
CURL * handle = curl_easy_init();
CURLcode result;
std::string url, contents;
std::cin >> url;
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, &errorBuf[0]);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &contents);
while(true)
{
    time_t startOfLoop = time(0);
    //Ping page
    result = curl_easy_perform(handle);
    if(result == 0)
        std::cout << "[" << time(0) << "]: Pinged "" << url << "" ok!rn";
    else
    {
        std::cout << "[" << time(0) << "]: Pinged "" << url << "" **ERROR: " << errorBuf << " [Error Code: " << result << "]rn";
    }
    //Pause until 5 seconds has passed
    while(difftime(time(0), startOfLoop) < 5) {}
}
curl_easy_cleanup(handle);

然后PHP文件写入MySQLi数据库,说程序已经ping到。此页面在应用程序中每 5 秒加载一次,并且工作正常。

但是,在一段时间后,网站将停止接受请求。http://www.downforeveryoneorjustme.com/说该网站仍然在线,但我得到了Oops! Google Chrome could not connect,并且 curl 应用程序只是在一段时间内吐出**ERROR: Failed connect to [site-url]:80; No error [Error Code: 7],直到网站决定再次开始允许请求。

这是我使用的虚拟主机有问题吗?(000webhost),我只需要升级到带宽更大的更好的虚拟主机吗?

还是我走错了路?有没有更好的方法可以从 c++ 应用程序联系我的网站?

任何帮助将不胜感激。
谢谢!

在我看来,

您描述的内容听起来像是服务器池中的所有可用TCP套接字都已用完(例如,进入已建立或TIME_WAIT状态)。 一旦套接字关闭,它会在一段时间(通常为 4 分钟)内进入TIME_WAIT状态。 在此时间之后,可以再次使用。 因此,如果您的服务器配置了非常少量的套接字(<250),并且 curl 每次都使用一个新套接字,那么这可能是问题所在。

如果您具有访问权限,则可以在服务器上运行netstat以检查套接字的当前状态。

但是,默认情况下,curl 应使用持久的保持活动连接。 根据文档,curl_easy_perform应该尝试重用连接,因此理论上您不应该看到此问题(除非服务器未配置为使用我想的保持活动)。

另外,您的代码有一些错误,所以我想知道是否会发生一些未定义的行为。 我建议修复这些错误,看看问题是否仍然存在。

  1. curl_easy_setopt(handle, CURLOPT_URL, url);中使用url.c_str()而不是url

  2. 传递指向缓冲区的指针,而不是 curl_easy_setopt(handle, CURLOPT_WRITEDATA, &contents); 中的字符串

  3. 根据curl_easy_perform的文档,curl_easy_setopt应该在每次调用curl_easy_perform之前调用,所以尝试在循环内移动setopts。

我将您的代码复制到测试应用程序中,并对其进行修改以在我的系统上进行测试。它工作正常。 这是我的完整代码:

#include <iostream>
#include <string>
#include <thread>
#include <curl/curl.h>
using namespace std;
char errorBuf[CURL_ERROR_SIZE];
char contents[CURL_MAX_WRITE_SIZE];
std::string url;
size_t received_len = 0;
size_t write_func( char *ptr, size_t size, size_t nmemb, void *userdata)
{
    size_t len = size*nmemb;
    received_len += len;
    return len;
}
int main()
{
    curl_global_init(CURL_GLOBAL_ALL);
    CURL * handle = curl_easy_init();
    CURLcode result;
    cout << "Enter URL:";
    cin >> url;
    while(true)
    {
        received_len = 0;
        // Set options before each call to curl_easy_perform()
        curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
        curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errorBuf);
        curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_func);
        curl_easy_setopt(handle, CURLOPT_WRITEDATA, contents);
        // Invoke the URL
        if((result = curl_easy_perform(handle)) == 0)
            cout << "[" << time(0) << "]: "" << url << "" Received " << received_len << " bytes ok!n";
        else
            std::cout << "[" << time(0) << "]: "" << url << "" **ERROR: " << errorBuf << " [Error Code: " << result << "]n";
        // Pause until 5 seconds has passed
        std::this_thread::sleep_for(std::chrono::seconds(5));
    }
    // NOTE: This will never be called
    curl_easy_cleanup(handle);
    return 0;
}