为什么我的CURL发布了我的请求两次或为什么我的流包含重复的请求消息?

时间:2017-01-04 16:02:04

标签: c++ sockets curl groovy

您好我已用c ++编写代码以使用cURL发布请求。此外,我还编写了一个代码,在groovy中接收此请求。在测试代​​码时,似乎curl重新发布请求。但我有点不确定,因为它没有反映在我的卷曲痕迹中。另一方面,我的套接字流反映了这一点。我猜不出问题出在哪里。

期望:

服务器端: 如果服务器收到某个特定请求,它会发送一些响应消息,如果不是,它只会发送一些整数(为了测试它现在是整数)。

客户方: 它会偶尔发送请求并获得响应,如果没有要求它只会听取接收消息(例如那些整数)

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 Server:

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

--------&GT;如果我终止客户端仍然等待接收进一步的消息,它将再收到一条消息并开始向客户发送消息

杀死客户端后跟踪

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 个答案:

没有答案