curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: Detection of failed connection

From: Ali Nasir via curl-library <curl-library_at_lists.haxx.se>
Date: Wed, 3 Jan 2024 16:04:43 +0530

Sorry for that link. The base code used was from
https://curl.se/libcurl/c/multi-post.html. It is modified as below:
if(curl)
    {
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        multi_handle = curl_multi_init();
        /* Create the form */
        form = curl_mime_init(curl);

        /* Fill in the file upload field */
        field = curl_mime_addpart(form);
        curl_mime_name(field, "files");
        curl_mime_filedata(field, fileName);

        /* initialize custom header list (stating that Expect: 100-continue
is not
           wanted */
        headerlist = curl_slist_append(headerlist, buf);
        /* what URL that receives this POST */
        curl_easy_setopt(curl, CURLOPT_URL, webUrl);
        curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
        list = curl_slist_append(list, "Shoesize: 10");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
        curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);

        curl_multi_add_handle(multi_handle, curl);

        while(still_running)
        {
            CURLMsg *msg;
            int queued;
            CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
            printf("\n\rMulti Perform exit with code %d, still running
%d\n\r", mc, still_running);
            if(still_running)
            {
              /* wait for activity, timeout or "nothing" */
              mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
            }

            printf("\n\rMulti poll : %d", mc);
            if(mc)
              break;

            do
            {
              msg = curl_multi_info_read(multi_handle, &queued);
              printf("\n\r Queued %d \n\r",queued);
              if(msg)
              {
                printf("\n\rCurl code %d\n\r", msg->data.result);
                if(msg->msg == CURLMSG_DONE)
                {
                  /* a transfer ended */
                  fprintf(stderr, "Transfer completed\n");
                }
              }
            } while(msg);
        }

        res = curl_easy_getinfo(curl, CURLINFO_OS_ERRNO, &error);
        printf("\n\rERRNO %ld\n\r", error);
        curl_multi_remove_handle(multi_handle, curl);
        curl_multi_cleanup(multi_handle);
        /* always cleanup */
        curl_easy_cleanup(curl);
        /* then cleanup the form */
        curl_mime_free(form);
        /* free slist */
        curl_slist_free_all(headerlist);
    }
    printf("\n\r");
    return 0;
}

In the absence of the server running, below is the output on the console:
* Initializing NSS with certpath: none

Multi Perform exit with code 0, still running 1

Multi poll : 0
 Queued 0

Multi Perform exit with code 0, still running 1

Multi poll : 0
 Queued 0
* Trying 192.168.65.2:80...
* TCP_NODELAY set

Multi Perform exit with code 0, still running 1

Multi poll : 0
 Queued 0

Multi Perform exit with code 0, still running 1

Multi poll : 0
 Queued 0
* Connected to host.docker.internal (192.168.65.2) port 80 (#0)
> POST /upload HTTP/1.1
Host: host.docker.internal
Accept: */*
Shoesize: 10
Content-Length: 321
Content-Type: multipart/form-data;
boundary=------------------------b2b856525299ec0a

* We are completely uploaded and fine

Multi Perform exit with code 0, still running 1

Multi poll : 0
 Queued 0

Multi Perform exit with code 0, still running 1

Multi poll : 0
 Queued 0

Multi Perform exit with code 0, still running 1

Multi poll : 0
 Queued 0

Multi Perform exit with code 0, still running 1

Multi poll : 0
 Queued 0
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 403 connecting to host.docker.internal:80: connecting to
127.0.0.1:80: dial tcp 127.0.0.1:80: connectex: No c
onnection could be made because the target machine actively refused it.
< Connection: close
<
connecting to host.docker.internal:80: connecting to 127.0.0.1:80: dial tcp
127.0.0.1:80: connectex: No connection could be made because t
he target machine actively refused it.
Multi Perform exit with code 0, still running 1
Multi poll : 0
 Queued 0
* Closing connection 0
Multi Perform exit with code 0, still running 0
Multi poll : 0
 Queued 0
Curl code 0
Transfer completed
 Queued 0
ERRNO 0

If we see the output, all the methods are returning 0, although there is a
message (due to verbose enabled) that the machine refused the connection.

On Wed, Jan 3, 2024 at 2:02 PM Ray Satiro via curl-library <
curl-library_at_lists.haxx.se> wrote:

> On 1/3/2024 3:17 AM, Ali Nasir via curl-library wrote:
>
> I am trying to upload a file to an end point using multi interface.
> Example code used is https://curl.se/libcurl/c/postit2.html
> However, i see that when the server is down, there is no error reported
> from any of the curl interfaces.
>
> If i perform the same operation via the curl command from the shell, i get
> the return code as 7. But all the multi interfaces like info_read and
> mulit_perform return CURLE_OK.
> Can anyone guide how to detect failures with libcurl. Specially when the
> server is down.
>
>
> That example uses curl_easy_perform not curl_multi_perform so there must
> be some changes that you made. curl_multi_info_read should return the
> transfer result code (CURLcode) in data.result. [1][2]
>
> Also you could enable verbose mode to see if that's how the transfer is
> actually failing. [3]
>
> [1]: https://curl.se/libcurl/c/curl_multi_info_read.html
> [2]: https://curl.se/libcurl/c/libcurl-errors.html
> [3]: https://curl.se/libcurl/c/CURLOPT_VERBOSE.html
>
>
> --
> Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-library
> Etiquette: https://curl.se/mail/etiquette.html
>


-- 
Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html
Received on 2024-01-03