Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example/maxconnects: set maxconnects example #11343

Closed
wants to merge 1 commit into from

Conversation

xquery
Copy link
Member

@xquery xquery commented Jun 19, 2023

No description provided.

@xquery xquery self-assigned this Jun 19, 2023
@xquery xquery changed the title example/maxconnects: set maxconnect example example/maxconnects: set maxconnects example Jun 19, 2023
@bagder
Copy link
Member

bagder commented Jun 20, 2023

Since maxconnects limits the number of connections kept alive, maybe this example should just loop over 2 or three different URLs so that a user could see (in the verbose output) that it will close connections that otherwise would have been kept alive?

@bagder
Copy link
Member

bagder commented Jul 9, 2023

Here's my proposed extended version:

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) James Fuller, <jim@webcomposite.com>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 * SPDX-License-Identifier: curl
 *
 ***************************************************************************/
/* <DESC>
 * Set maximum number of persistent connections to 1.
 * </DESC>
 */
#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    const char *urls[] = { "https://example.com",
      "https://curl.se",
      "https://www.example/",
      NULL /* end of list */
    };
    int i = 0;

    /* Change the maximum number of persistent connection   */
    curl_easy_setopt(curl, CURLOPT_MAXCONNECTS, 1L);

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    /* loop over the URLs */
    while(urls[i]) {
      curl_easy_setopt(curl, CURLOPT_URL, urls[i]);

      /* Perform the request, res will get the return code */
      res = curl_easy_perform(curl);
      /* Check for errors */
      if(res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));
      i++;
    }
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

@bagder bagder closed this in 97394ca Jul 11, 2023
@bagder bagder deleted the examples/maxconnects branch July 11, 2023 18:13
bch pushed a commit to bch/curl that referenced this pull request Jul 19, 2023
ptitSeb pushed a commit to wasix-org/curl that referenced this pull request Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

2 participants