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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:38:04+00:00 2026-05-16T16:38:04+00:00

I would like to know what would be most efficient when checking for incoming

  • 0

I would like to know what would be most efficient when checking for incoming data (asynchronously). Let’s say I have 500 connections. I have 3 scenarios (that I can think of):

  1. Using select() to check FD_SETSIZE sockets at a time, then iterating over all of them to receive the data. (Wouldn’t this require two calls to recv for each socket returned? MSG_PEEK to allocate a buffer then recv() it again which would be the same as #3)
  2. Using select() to check one socket at a time. (Wouldn’t this also be like #3? It requires the two calls to recv.)
  3. Use recv() with MSG_PEEK one socket at a time, allocate a buffer then call recv() again. Wouldn’t this be better because we can skip all the calls to select()? Or is the overhead of one recv() call too much?

I’ve already coded the situations to 1 and 2, but I’m not sure which one to use. Sorry if I’m a bit unclear.

Thanks

  • 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-05-16T16:38:05+00:00Added an answer on May 16, 2026 at 4:38 pm

    …most efficient when checking for
    incoming data (asynchronously). Let’s
    say I have 500 connections. I have 3
    scenarios (that I can think of):

    Using select() to check FD_SETSIZE
    sockets at a time, then iterating over
    all of them to receive the data.
    (Wouldn’t this require two calls to
    recv for each socket returned? MSG_PEEK to allocate a buffer then recv() it again which would be the same as #3)

    I trust you’re carefully constructing your fd set with only the descriptors that are currently connected…? You then iterate over the set and only issue recv() for those that have read or exception/error conditions (the latter difference being between BSD and Windows implementations). While it’s ok functionally (and arguably elegant conceptually), in most real-world applications you don’t need to peek before recv-ing: even if you’re unsure of the message size and know you could peek it from a buffer, you should consider whether you can:

    • process the message in chunks (e.g. read whatever’s a good unit of work – maybe 8k, process it, then read the next <=8k into the same buffer…)
    • read into a buffer that’s big enough for most/all messages, and only dynamically allocate more if you find the message is incomplete

    Using select() to check one socket at a time. (Wouldn’t this also be like #3? It requires the two calls to recv.)

    Not good at all. If you stay single-threaded, you’d need to put a 0 timeout value on select and spin like crazy through the listenig and client descriptors. Very wasteful of CPU time, and will vastly degrade latency.

    Use recv() with MSG_PEEK one socket at a time, allocate a buffer then call recv() again. Wouldn’t this be better because we can skip all the calls to select()? Or is the overhead of one recv() call too much?

    (Ignoring that it’s better to try to avoid MSG_PEEK) – how would you know which socket to MSG_PEEK or recv() on? Again, if you’re single threaded, then either you’d block on the first peek/recv attempt, or you use non-blocking mode and then spin like crazy through all the descriptors hoping a peek/recv will return something. Wasteful.

    So, stick to 1 or move to a multithreaded model. For the latter, the simplest approach to begin with is to have the listening thread loop calling accept, and each time accept yields a new client descriptor it should spawn a new thread to handle the connection. These client-connection handling threads can simply block in recv(). That way, the operating system itself does the monitoring and wake-up of threads in response to events, and you can trust that it will be reasonably efficient. While this model sounds easy, you should be aware that multi-threaded programming has lots of other complications – if you’re not already familiar with it you may not want to try to learn that at the same time as socket I/O.

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

Sidebar

Related Questions

I would like to know the most efficient way of emptying an associative array
I would like to know the most efficient way of storing days of the
I would like to know what is the most efficient and practical way of
I would like to know to to write most efficient LINQ ( EDIT :
I would like to know what is the most efficient way to create a
I would like to know what is the most efficient way to transfer BitmapData
I know that this kind of issue is most discussed. But I would like
I would like to know how. I have looked at this topic , and
I have two handlers for a mouseover function, I would like to know what
I am implementing a loaner system and would like find out the most efficient

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.