Skip to content

Instantly share code, notes, and snippets.

@alghanmi
Created May 5, 2014 20:12
Show Gist options
  • Star 78 You must be signed in to star a gist
  • Fork 22 You must be signed in to fork a gist
  • Save alghanmi/c5d7b761b2c9ab199157 to your computer and use it in GitHub Desktop.
Save alghanmi/c5d7b761b2c9ab199157 to your computer and use it in GitHub Desktop.
cURL C++ Example
#include <iostream>
#include <string>
#include <curl/curl.h>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
std::cout << readBuffer << std::endl;
}
return 0;
}
@PardeepSangha
Copy link

I developed a client which is receiving data from server occasionally. But when I receive first data, then program exists. I tried while loop like windows message loop, but it gives me last data.

How can I stay in program so that I can receive more messages from server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment