如何使用 c++ 对 php 页面进行 ping 操作
How to ping a php page using c++?
我希望能够向我的网站发出信号,表明我的 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应该尝试重用连接,因此理论上您不应该看到此问题(除非服务器未配置为使用我想的保持活动)。
另外,您的代码有一些错误,所以我想知道是否会发生一些未定义的行为。 我建议修复这些错误,看看问题是否仍然存在。
在
curl_easy_setopt(handle, CURLOPT_URL, url);
中使用url.c_str()
而不是url
传递指向缓冲区的指针,而不是
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &contents);
中的字符串- 根据
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;
}
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 重载操作程序时出错>>用于类中的字符串 memebr
- 对字符串进行位操作
- 我可以在 C++ 中的函数体之外进行操作吗?
- MPI突然停止了对多个核心的操作
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 对字符数组中的元素执行逐位操作
- 如何在directx/c++中进行平移/缩放操作
- 逐位操作的隐式类型转换
- 为什么一个向量上的多线程操作很慢
- 排序时无法执行交换操作.我做的时候它会崩溃.为什么
- 位移操作和位掩码未检测到重复字符
- 如何进行特定的位操作?
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 字符串操作 - 字符计数
- 此代码中的操作流程是什么?C/C++.
- 复制和交换习惯用法与移动操作之间的交互
- 像union_这样的 Boost.Geometry 操作如何处理浮点类型的基本不精确性?
- 如何使用 c++ 对 php 页面进行 ping 操作
- Linux中具有ping操作的Qt线程