In a C ++ program that uses libCurl I am sending an http message to another
machine running a REST server written in C ++. This server sends a response to
the received message and the client collects said response.
The source code of the client program is the one attached below with the name
"FULL PROGRAM".
This program is capable of sending the message to the server and of collecting
the response from it, but as it is written, it sends the message twice to the
server, despite the fact that in the C ++ code it is only being sent once (it
must be something that the libcurl library does independently of the C ++
program).
I have observed that if I remove the part of the code where the response is
received from the server, then the message is only sent once from the client to
the server. (Corresponds to "PROGRAM 2 (ONLY SEND MESSAGE)").
In this particular application, it is important that the message is only sent
once and the response is received, so PROGRAM 2 does not serve as a solution
and I must get "FULL PROGRAM" to work correctly.
I have been looking at various forums on the internet, but I have not been able
to fix it.
I would appreciate any help with this topic.
---------------------------------------------------------------------------------
FULL PROGRAM
---------------------------------------------------------------------------------
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <sstream>
#include <curl/curl.h>
size_t getAnswerFunction(void* ptr, size_t size, size_t nmemb, std::string*
data) {
data->append((char*)ptr, size * nmemb);
return size * nmemb;
}
void sendDataAndGetAnswer(std::string data)
{
CURL *curl;
CURLcode res;
struct curl_slist *httpHeaders=NULL;
curl = curl_easy_init();
if (curl)
{
// curl_global_init(CURL_GLOBAL_ALL);
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.100:15000");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 1000);
// .........................................
// MyProgram sends data.
// This works right.
// .........................................
res = curl_easy_perform(curl);
if (res != CURLE_OK)
std::cout << "curl_easy_perform() failed to send message: " <<
curl_easy_strerror(res) << std::endl;
else
std::cout << "Error sening message." << std::endl;
// .........................................
// MyProgram get answer.
// This does not work.
// .........................................
std::string response_string;
// curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.100:15000");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getAnswerFunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
// getAnswerFunction waits at most 5000 ms.
// Afterwards, the flow of the program should continue even if there is no
response.
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 5000);
res = curl_easy_perform(curl);
if (res == CURLE_OK)
std::cout << "The answer from server is: " << response_string;
else
std::cout << "curl_easy_perform() failed to get answer: " <<
curl_easy_strerror(res) << std::endl;
curl_easy_cleanup(curl);
curl_slist_free_all(httpHeaders);
}
}
int main(void)
{
while (1)
{
int valueVar = 0;
// Execute various statements and perform calculations.
// ...
// Calculate valueVar here.
// ...
valueVar = 5;
std::string msgToSend("CONTITION-IS-TRUE");
if (valueVar == 5)
sendDataAndGetAnswer(msgToSend);
sleep(10);
}
return 0;
}
---------------------------------------------------------------------------------
PROGRAM 2 (ONLY SEND MESSAGE)
(Only the sendDataAndGetAnswer function changes, the rest is the same)
---------------------------------------------------------------------------------
void sendDataAndGetAnswer(std::string data)
{
CURL *curl;
CURLcode res;
struct curl_slist *httpHeaders=NULL;
curl = curl_easy_init();
if (curl)
{
// curl_global_init(CURL_GLOBAL_ALL);
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.100:15000");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 1000);
// .........................................
// MyProgram sends data.
// This works right.
// .........................................
res = curl_easy_perform(curl);
if (res != CURLE_OK)
std::cout << "curl_easy_perform() failed to send message: " <<
curl_easy_strerror(res) << std::endl;
else
std::cout << "Error sening message." << std::endl;
curl_easy_cleanup(curl);
curl_slist_free_all(httpHeaders);
}
}
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html