Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8257825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:19:47+00:00 2026-06-08T02:19:47+00:00

Beej’s Simple Client example code iterates over all IP addresses returned from getaddrinfo(), until

  • 0

Beej’s Simple Client example code iterates over all IP addresses returned from getaddrinfo(), until it can connect to the first one. See the code below.

Is this always necessary, or is it OK to assume that we only have to try to connect to the first address returned by getaddrinfo()?

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

if ((rv = getaddrinfo(argv[1], PORT, &hints, &servinfo)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    return 1;
}

// ------------------------------------------------------------
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
    if ((sockfd = socket(p->ai_family, p->ai_socktype,
            p->ai_protocol)) == -1) {
        perror("client: socket");
        continue;
    }

    if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
        close(sockfd);
        perror("client: connect");
        continue;
    }

    break;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:19:48+00:00Added an answer on June 8, 2026 at 2:19 am

    Yes, you should iterate over all the addresses – in particular, consider the case when the destination host has IPv6 addresses enabled but your local host does not. getaddrinfo() will return AF_INET6 family addresses, but then either the socket() or connect() call will fail.

    It’s also a possibility that your host supports multiple protocols implementing SOCK_STREAM (say, SCTP in addition to TCP) and the destination host does not – since you haven’t set the ai_protocol member of the hints structure, addresses representing all protocols supporting SOCK_STREAM sockets will be returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From Beej's Guide to Network programming You can force it to use IPv4 or
I'm working through Beej's socket tutorial . Using the sample code I've created a
This is from Beej's guide to C The drawback to using calloc() is that
I've been using getaddrinfo for looking up socket addresses for basic socket commands. Recently,
I modified beej's guide to networking example (as seen below) to pass back a
I read Beej's guide and created a UDP server and client. I first run
I would like to write an application to detect the IP addresses of all
I was checking Beej's guide to IPC and one line of code took my
I've been reading through Beej's Guide to Network Programming to get a handle on
I'm trying to compile beej's guide to network programming examples, but Windows XP doesn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.