Hi, this happens because you do return nmemb; in write_data() instead of return nmemb*size; since that is the actual data size in bytes, so curl_easy_perform is waiting for you to consume all data and thus hangs since you never do (or rather don't tell curl that you do).
/HH Den mån 24 okt. 2022 kl 22:40 skrev Hamza Farooq via curl-library < curl-library@lists.haxx.se>: > Hello, > > I am using libcurl for receiving mqtt data (subscribe) with following > configuration: > > curl_easy_setopt( g_curl, CURLOPT_VERBOSE, 0 ); /* set verbosity */ > curl_easy_setopt( g_curl, CURLOPT_URL, buf ); /* Set the URL/path */ > curl_easy_setopt( g_curl, CURLOPT_PORT, g_port ); /* Set the port */ > curl_easy_setopt( g_curl, CURLOPT_DEFAULT_PROTOCOL, "mqtt"); /* if URL > misses scheme */ > curl_easy_setopt( g_curl, CURLOPT_NOPROGRESS, 1); /* turn off progress > meter */ > curl_easy_setopt( g_curl, CURLOPT_TIMEOUT, 30); /* connection timeout */ > curl_easy_setopt( g_curl, CURLOPT_WRITEFUNCTION, write_data); > curl_easy_setopt( g_curl, CURLOPT_WRITEDATA, user_data); > > cc = curl_easy_perform(g_curl); > curl_easy_cleanup(g_curl); > if (cc != CURLE_OK) { > fprintf( stderr, "could not get MQTT data (%d)\n", > cc ); > return sz; > } > > Here is what my write_data() looks like: > > size_t write_data(char *in_data, size_t size, size_t nmemb, void > *user_data){ > > fprintf( stdout, "entered write_data\n"); > fprintf( stdout, "Received msg: %s\n", in_data+2); > > //perform data operations > return nmemb; > } > > Problem: > I receive the data in my write_data function, but the > curl_easy_perform() call in the main function does not return. It only > returns after the timeout is over, regardless of how many times the > data is published by the MQTT broker and received by the app. Is this > expected behavior? How can I make curl_easy_perform return? > > PS: If there is any sample code for libcurl that uses MQTT, please > point me in that direction. > > Best regards, > Hamza > -- > Unsubscribe: https://lists.haxx.se/listinfo/curl-library > Etiquette: https://curl.se/mail/etiquette.html >
-- Unsubscribe: https://lists.haxx.se/listinfo/curl-library Etiquette: https://curl.se/mail/etiquette.html