-
-
Save alghanmi/c5d7b761b2c9ab199157 to your computer and use it in GitHub Desktop.
| #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; | |
| } |
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.
@alghanmi Hi!
First off, thank you for sharing your code example! It was incredibly helpful for getting me started quickly. However, I wanted to point out a couple of discrepancies that I noticed:
- Missing global initialization:
The libcurl documentation recommends using curl_global_init() at the beginning of the program to set up the necessary environment for curl operations. See "Global preparation" section:
- https://curl.se/libcurl/c/libcurl-tutorial.html - Callback prototype mismatch:
The prototype for the callback function seems to be inconsistent with the one documented on the official libcurl site. The prototype for this callback function is described here:
- https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);On a related note, I've created a more detailed setup documentation and added a simple example as well as a slightly more complex one that includes POST requests, which some may find useful:
- https://github.com/voidlib/cpp-curl-example
Thanks again for your contribution—it was a great starting point for me!
Best regards,
@voiduin
this was the best explanation
I'm getting this output
$ g++ main.cpp -lcurl
C:\Users\Admin\AppData\Local\Temp\ccEeyCDI.o:cleopatra.cpp:(.text+0x51): undefined reference to `_imp__curl_easy_init'
C:\Users\Admin\AppData\Local\Temp\ccEeyCDI.o:cleopatra.cpp:(.text+0x7b): undefined reference to `_imp__curl_easy_setopt'
C:\Users\Admin\AppData\Local\Temp\ccEeyCDI.o:cleopatra.cpp:(.text+0x98): undefined reference to `_imp__curl_easy_setopt'
C:\Users\Admin\AppData\Local\Temp\ccEeyCDI.o:cleopatra.cpp:(.text+0xb4): undefined reference to `_imp__curl_easy_setopt'
C:\Users\Admin\AppData\Local\Temp\ccEeyCDI.o:cleopatra.cpp:(.text+0xc1): undefined reference to `_imp__curl_easy_perform'
C:\Users\Admin\AppData\Local\Temp\ccEeyCDI.o:cleopatra.cpp:(.text+0xd1): undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
@hasirciogli
-lcurllibrary when you compiled, hence the "unresolved external symbol" errors.https://example.comdoes not support--http3, yet; you could test this using any HTTP/3 verifier.Try using any of the HTTP/3 supported websites, some are mentioned here.
https://quic.techbut received acurl_easy_perform() failed: SSL connect errorerror.Most probably because I don't have the OpenSSL libraries installed, and this is expected.
But the code itself is executing fine.
Hope that helps :)