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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:48:48+00:00 2026-06-04T17:48:48+00:00

I have a function which receives data from socket, i need to stop function

  • 0

I have a function which receives data from socket, i need to stop function when 5 second passed without creating additional thread. My code:

void TestReceive()
{
 //how to stop code below when 5 second passed&
 size_t received = 0;
 while (received < 4)
 {
  ssize_t r = read(fd, buffer, 4);
  if (r <= 0) break;
  received += r;
 }
}
  • 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-04T17:48:50+00:00Added an answer on June 4, 2026 at 5:48 pm

    Use select(), this will block until something is available to read or at most for the time specified in the timeval as below.

    int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
    

    select() does not read or write, what it does is for a set of descriptors (readfds, writefds, and/or exceptfds), it will wait/block until one of the following happens:

    • there is a descriptor ready to be read from
    • there is a descriptor ready to be written to
    • there is an exception on one of the descriptors, like it was closed
    • if the timeout is specified, when the configured time expires

    If you just simply need to “wait” (sleep) for 5 seconds, then read, you could sleep (usleep() on linux) for 5 seconds, then do a non-blocking read either by setting the socket options, or call select() with the minimum timeout and check if there is anything to be read.

    Here’s a related question. How C++ select() function works in Unix OSs?

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

Sidebar

Related Questions

Hey guys I have an ajax jquery function that receives data from a php
I have a function which sends a command over serial, then receives a response
I have this function which cuts a string into a number of characters without
I have a web server which receives an image from a client and do
I have a form, in which I need to send data selected via AJAX
I have a javascript which receives info from a servlet using jQuery: $.get(authenticate, {badge:$('input#badge').val()},
i have a TCP server which receives requests from clients. i want to make
hi i have function which is called by tinker listbox so i cannot return
I have function getMemory() which returns VARIANT (mfc). It is said that in ulVal
I have a function which calls several other functions, e.g. sub do_work { send_mail();

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.