Почему мой CURL публикует мой запрос дважды или почему мой поток содержит сообщение с повторным запросом?

Привет, я написал код на C ++, чтобы опубликовать запрос с помощью cURL. А также я написал код, который получает этот запрос в Groovy. При тестировании кода кажется, что curl делает репост запроса. Но я немного не уверен, так как это не отражается на моем следе локона. С другой стороны, мой поток сокетов отражает это. Я не мог догадаться, в чем проблема.

Expectation:

Сторона сервера:
Если сервер получит какой-то определенный запрос, он отправит ответное сообщение, если нет, он просто отправит некоторое целое число (для проверки это теперь целое число).

Сторона клиента:
Время от времени он отправляет запрос и получает ответ, если ему нечего запрашивать, он просто прослушивает получение сообщения (например, эти целые числа)

C ++ Curl Call:

 if(curl){

curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION, 1);

string Mydata;

char errbuf[CURL_ERROR_SIZE];
chunk.memory = (char*)malloc(1);  /* will be grown as needed by the realloc above */
chunk.size = 0;    /* no data at this point */
FILE * rfp = fopen("/Users/Uma/request.xml", "r");

if(rfp != NULL){
cout<<"File is opened "<<endl;
}else{
cout<<"File is close"<<endl;

}

curl_easy_setopt(curl, CURLOPT_URL, myUrl);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);

const char* req= "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:blz=\"http://thomas-bayer.com/blz/\">  <soapenv:Header/> <soapenv:Body>   <blz:getBank>  <blz:blz>50070010</blz:blz> </blz:getBank>  </soapenv:Body>  </soapenv:Envelope> \n";

curl_easy_setopt(curl,CURLOPT_HTTPPOST, 1L);

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req);

cout<<"requested posted "<<endl;

curl_easy_setopt(curl, CURLOPT_READFUNCTION, reader);
curl_easy_setopt(curl, CURLOPT_READDATA, rfp);

errbuf[0] = 0;

/* send all data to this function  */

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&chunk);

curlCode = curl_easy_perform(curl);
//  always cleanup
if(curlCode != CURLE_OK  | curlCode != 0) {

std::cout<<"  traced data "<<traceData<<std::endl;

size_t len = strlen(errbuf);
fprintf(stderr, "\nlibcurl: (%d) ", curlCode);
if(len)
fprintf(stderr, "%s%s", errbuf,
((errbuf[len - 1] != '\n') ? "\n" : ""));
else
fprintf(stderr, "%s\n", curl_easy_strerror(curlCode));
} else {

std::cout<<"CURl Code "<<curlCode<<std::endl;

/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*/
std::cout<<"CURL code on error --"<<curlCode<<std::endl;
printf("%lu bytes retrieved\n", (long)chunk.size);

//cout<<"My Response ---"<<response<<endl;

cout<<"My Chunck -- "<<chunk.memory<<endl;
}

curl_easy_cleanup(curl);
}

Groovy Сервер:

def server = new ServerSocket(2000)
println("Waiting for connection")

String receivedRecord
while (true)
{
println " Input data "
server.accept() { socket ->
socket.withStreams { input, output ->
def w = new BufferedWriter(new OutputStreamWriter(output))

String message = "Connection was successful"println message
def r = new BufferedReader(new InputStreamReader(input))

println ("Stream ")

String requestMessage = ""
def cont = true
String tempMsg;
while (cont)
{
tempMsg = r.readLine()

if(tempMsg == null)
{

cont = false

}
else
{

requestMessage = requestMessage  +   tempMsg
}

println (requestMessage)
println(cont)

tempMsg = " "}

int  i = 0;

while (i <20)
{
//String requestMessage = r.readLine()

//println(" Request Received ")
//println(requestMessage)

String echo = "EchoRequestMsg"String vehicleSync = "EenheidSynchronisatieRequestMsg"
if(requestMessage != null )
{
if(requestMessage.toLowerCase().contains(echo.toLowerCase()))
{
println (i)
println ("Echo Request True ")
sendMessage("Echo", w)
println(" Echo Message Sent ")

}
else if(requestMessage.toLowerCase().contains(vehicleSync.toLowerCase()))
{
println (i)
println (" Vehicle Sync True ")
sendMessage("VehicleSync", w)
println(" Vehicle Sync Message Sent ")
}
else
{
println (i)
println (" push message ")
i++;
sendMessage(i, w)
}

}}
}
}
}

def sendMessage(msg, writer) {
println("Sending: >" + msg + "<")
writer.writeLine(msg.toString())
writer.flush();
}

Выход сервера:

 Input data
Connection was successful
Stream
POST / HTTP/1.1
true
POST / HTTP/1.1Host: localhost:2000
true
POST / HTTP/1.1Host: localhost:2000Accept: */*
true
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xml
true
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xmlSOAPAction:urn:getBank
true
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xmlSOAPAction:urn:getBankContent-Length: 251
true
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xmlSOAPAction:urn:getBankContent-Length: 251
true
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xmlSOAPAction:urn:getBankContent-Length: 251<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:blz="http://thomas-bayer.com/blz/">  <soapenv:Header/> <soapenv:Body>   <blz:getBank>  <blz:blz>50070010</blz:blz> </blz:getBank>  </soapenv:Body>  </soapenv:Envelope>
true

———————>> все еще жду получения следующего сообщения, если я завершу работу с клиентом, он получит еще одно сообщение и начнет отправлять сообщение клиенту

След после того, как я убью клиента

groovy groovyServer.groovy
Waiting for connection
Input data
Input data
Connection was successful
Stream
POST / HTTP/1.1
POST / HTTP/1.1Host: localhost:2000
POST / HTTP/1.1Host: localhost:2000Accept: */*
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xml
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xmlSOAPAction:urn:getBank
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xmlSOAPAction:urn:getBankContent-Length: 256
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xmlSOAPAction:urn:getBankContent-Length: 256
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xmlSOAPAction:urn:getBankContent-Length: 256<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:blz="http://thomas-bayer.com/blz/">  <soapenv:Header/> <soapenv:Body>   <blz:getBank>  <blz:blz>50070010</blz:blz> </blz:getBank>  </soapenv:Body>  </soapenv:Envelope>
POST / HTTP/1.1Host: localhost:2000Accept: */*content-type:text/xmlSOAPAction:urn:getBankContent-Length: 256<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:blz="http://thomas-bayer.com/blz/">  <soapenv:Header/> <soapenv:Body>   <blz:getBank>  <blz:blz>50070010</blz:blz> </blz:getBank>  </soapenv:Body>  </soapenv:Envelope>
0
push message
Sending: >1<
1
push message
Sending: >2<
Exception in thread "Thread-1" java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
at java.io.BufferedWriter.flush(BufferedWriter.java:254)

Обратите внимание, что после того, как я убил клиента, добавлено еще одно сообщение с запросом. Он просто расположен прямо над запуском сообщения.

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]