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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:41:17+00:00 2026-06-09T03:41:17+00:00

I have a WinSock mySocket that receives in a loop like so: while(keepGoing){ FD_SET

  • 0

I have a WinSock mySocket that receives in a loop like so:

while(keepGoing){
    FD_SET fsd;
    FD_ZERO(&fds);
    FD_SET(mySocket, &fds);

    int result = select(1+mySocket, &fds, (fd_set *)0, (fd_set *)0, &timeout);
    if(result>0 && FD_ISSET(mySocket, &fds){
        result = recvfrom(mySocket, buff, NUM_BYTES, (struct sockaddr *)&add, &length);
        // do stuff       
    }
}

If I want to stop receiving I can set keepGoing to false, but the socket may still be waiting for timeout before it sees that keepGoing has changed.

Assuming that we do not want to change the value of the timeout, is there a reliable way to tell the socket to stop receiving without waiting for the timeout to occur?

  • 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-09T03:41:20+00:00Added an answer on June 9, 2026 at 3:41 am

    You can raise a benign signal and check the keepGoing flag when the errno is EINTR. Or, you can create a pipe and add the read end to your select set. Close the write end after setting the keepGoing flag.

    int quitfds[2];
    pipe(quitfds);
    
        //...
        FD_SET(quitfds[0], &fds);
        //...
    
        if (FD_ISSET(quitfds[0], &fds)) {
            close(quitfds[0]);
            continue;
        }
    

    Edit: Adam points out that I missed the Winsock reference in the question. You can use a socketpair instead of a pipe. It doesn’t seem to be implemented on Windows, but I found a workaround here. Ben points out that WSAEventSelect allows mixing of different event objects. Thanks to both.

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

Sidebar

Related Questions

I have a non-blocking winsock socket that is recv 'ing data in a loop.
i have a winsock component made with vb.net in a class library that has
I have a program in C++ using winsock that connects to a server, the
I have two programs written in C++ that use Winsock. They both accept TCP
I have a problem with the Winsock control. The problem is that I can't
I have a program that does some networking using Winsock, and one of our
I have a problem with a piece of legacy c++/winsock code that is part
I have just converted a program that uses winsock to unicode, and im running
I have manualy writing class for WinSock. My program have more that one threads.
I have had nothing but trouble with Winsock since I started using it. I

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.