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

  • SEARCH
  • Home
  • 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 8642349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:45:06+00:00 2026-06-12T11:45:06+00:00

This is my first question on this site, having used it many times to

  • 0

This is my first question on this site, having used it many times to help me in my c++ coding.

I am only asking because i’ve hit a wall, and just don’t get whats happening, and think its a nice little mystery for someone to solve.

I have a client / server architecture, with 1 server and multiple clients.

The code snippet below works fine, apart from the fact that the only socket that blocks is the last one in mList, so if 2 clients are connected only the second one to be connected receives data.
This code is not my own, i am attempting to debug a segmentation fault that occurs when moving an if statement.

int SocketManager::block(int secs, int usecs)
{
int ret = 0;
int result = 0;
struct timeval tv;
fd_set set;
int max_sd = 0;

if (mList.empty())
{
  return -1;
}

tv.tv_sec = secs;
tv.tv_usec = usecs;

FD_ZERO(&set);

SocketList::const_iterator iter;
int n = 0;

for (iter = mList.begin(); iter != mList.end(); iter++)
{
  if ((*iter)->socket() > max_sd)
  {
    max_sd = (*iter)->socket();
  }

  FD_SET((*iter)->socket(), &set);
  n++;
}

errno = 0;
if ((result = select(max_sd + 1, &set, NULL, NULL, &tv)) > 0)
{
  ret = 1;
  Socket *s = NULL;
  for (iter = mList.begin(); iter != mList.end(); iter++)
  {
    if (FD_ISSET((*iter)->socket(), &set))
    {
      s = (*iter);
    }
  }

  if (s)
  {
    mLastSocketPtr = s;
    s->checkForData();
    mLastSocketPtr = NULL;
  }
}
else if (result == 0)
{
  // Timeout
  ret = 0;
}
else
{
  // Error
  ret = -1;
}

return ret;
}

The problem occurs with the line “if (FD_ISSET((*iter)->socket(), &set))”, when the if statement

if (s)
{
  mLastSocketPtr = s;
  s->checkForData();
  mLastSocketPtr = NULL;
}

is moved inside the FD_ISSET function like so

for (iter = mList.begin(); iter != mList.end(); iter++)
      {
        if (FD_ISSET((*iter)->socket(), &set))
        {
          s = (*iter);
          if (s)
          {
            mLastSocketPtr = s;
            s->checkForData();
            mLastSocketPtr = NULL;
          }
        }
      }

Any help?

  • 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-12T11:45:07+00:00Added an answer on June 12, 2026 at 11:45 am

    Most likely, the problem is that you are modifying mList while you are traversing it. By moving checkForData inside the for loop, any modification to mList inside checkForData is not allowed as it invalidates the iterator you are about to increment and dereference.

    There are a lot of ways to fix this code. The best way would be not to write code like this in the first place, but I guess that ship has sailed. Here are a few suggestions:

    1) Go through the FD_SET instead of going through mList. For each hit you find in the FD_SET, find the corresponding entry in mList. That way, you’ll be going through something that won’t be changing.

    2) When you get a hit in the FD_SET, clear the flag in the FD_SET. Start the for loop over with a fresh call to begin.

    3) Go through the list once and make a vector of all the sockets that need processing. Then iterate over that vector to call checkForData.

    Here’s an ugly fix showing method 2:

    for (iter = mList.begin(); iter != mList.end(); iter++)
      {
        if (FD_ISSET((*iter)->socket(), &set))
        {
          s = (*iter);
          FD_CLR(s->socket(), &set); // don't find this again
          mLastSocketPtr = s;
          s->checkForData();
          mLastSocketPtr = NULL;
          iter = mList.begin(); // make the iterator valid
          continue; // don't increment the iterator
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

though I have visited this site many times, this is my first question. After
Hello stackoverflow guru's! This is my first post after having used the site for
This is my first question, but I've already found this site extremely helpful, so
This is my first question. Thanks to everyone who contributes to this site, it's
this is my first question.. so, here we go. i have a site, 100%
This is my first time posting a question on this site, but certainly not
this is my first question in here, and I would like to ask if
This is my first question on Stack Overflow, so I hope that I'm clear
This is my first question, so please be gentle. I'm trying out Apache Camel
this is my first question to SO so i'll try not to disgrace myself.

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.