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 7822211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:51:40+00:00 2026-06-02T07:51:40+00:00

I’m using select() in a thread to monitor a datagram socket, but unless data

  • 0

I’m using select() in a thread to monitor a datagram socket, but unless data is being sent to the socket before the thread starts, select() will continue to return 0.

I’m mixing a little C and C++; here’s the method that starts the thread:

bool RelayStart() {
    sock_recv = socket(AF_INET, SOCK_DGRAM, 0);
    memset(&addr_recv, 0, sizeof(addr_recv));
    addr_recv.sin_family = AF_INET;
    addr_recv.sin_port = htons(18902);
    addr_recv.sin_addr.s_addr = htonl(INADDR_ANY);
    bind(sock_recv, (struct sockaddr*) &addr_recv, sizeof(addr_recv));

    isRelayingPackets = true;

    NSS::Thread::start(VIDEO_SEND_THREAD_ID);

    return true;
}

The method that stops the thread:

bool RelayStop() {
    isSendingVideo = false;
    NSS::Thread::stop();
    close(sock_recv);
    return true;
}

And the method run in the thread:

void Run() {

    fd_set read_fds;
    int select_return;
    struct timeval select_timeout;

    FD_ZERO(&read_fds);
    FD_SET(sock_recv, &read_fds);

    while (isRelayingPackets) {

        select_timeout.tv_sec = 1;
        select_timeout.tv_usec = 0;

        select_return = select(sock_recv + 1, &read_fds, NULL, NULL, &select_timeout);
        if (select_return > 0 && FD_ISSET(sock_recv, &read_fds)) {
            // ...
        }
    }
}

The problem is that if there isn’t a process already sending UDP packets to port 18902 before RelayStart() is called, select() will always return 0. So, for example, I can’t restart the sender without restarting the thread (in the correct order.)

Everything seems to work fine as long as the sender is started first.

  • 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-02T07:51:41+00:00Added an answer on June 2, 2026 at 7:51 am

    The Run thread only constructs read_fds once.

    The select call updates read_fds to have all its bits cleared for all descriptors that did not have data ready, and all its bits set for those that were set before and do have data ready.

    Hence, if no descriptor has any data ready and the select call times out (and returns 0), all the bits in read_fds are now cleared. Further calls passing the same all-zero bit-mask will scan no file descriptors.

    You can either re-construct the read-set on each trip inside the loop:

    while (isRelayingPackets) {
        FD_ZERO(&read_fds);
        FD_SET(sock_recv, &read_fds);
        ...
    }
    

    or use an auxiliary variable with a copy of the bit-set:

    while (isRelayingPackets) {
        fd_set select_arg = read_fds;
        ... same as before but use &select_arg ...
    }
    

    (Or, of course, there are non-select interfaces that are easier to use in some ways.)

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

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.