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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:36:41+00:00 2026-06-17T15:36:41+00:00

I am writing a Windows 7 visual c++ server application, which should receive UDP

  • 0

I am writing a Windows 7 visual c++ server application, which should receive UDP datagrams with 3.6 MB/s.
I have a main thread where the recvfrom() receives the data. The socket is a non-blocking socket and has 64kB receive buffer. If no data has been received on the socket the thread executes a sleep(1).

My problem is that the thread uses up almost 50% of my dual-core processor and I have no idea how could I decrease it. Wireshark use only 20% of it, so my main goal is to achieve a similar percentage.

Do you have any ideas?

  • 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-17T15:36:42+00:00Added an answer on June 17, 2026 at 3:36 pm

    Rather than polling you could use a select-like approach to wait for either data to arrive at your socket or the client to decide to shutdown:

    First make your socket non-blocking:

    u_long nonBlocking = 0;
    WSAEventSelect(sock, NULL, 0);
    ioctlsocket(sock, FIONBIO, &nonBlocking);
    

    then use WSAWaitForMultipleEvents to wait until either data arrives or you want to cancel the recv:

    int32_t MyRecv(THandle sock, WSAEVENT* recvCancelEvt,
                   uint8_t* buffer, uint32_t bufferBytes)
    {
        int32_t bytesReceived;
        WSAEVENT evt;
        DWORD ret;
        HANDLE handles[2];
    
        event = WSACreateEvent();
        if (NULL == evt) {
            return -1;
        }
        if (0 != WSAEventSelect(handle->iSocket, evt, FD_READ|FD_CLOSE)) {
            WSACloseEvent(evt);
            return -1;
        }
    
        bytesReceived = recv(sock, (char*)buffer, bufferBytes, 0);
        if (SOCKET_ERROR==received && WSAEWOULDBLOCK==WSAGetLastError()) {
            handles[0] = evt;
            handles[1] = *recvCancelEvt;
            ret = WSAWaitForMultipleEvents(2, handles, FALSE, INFINITE, FALSE);
            if (WAIT_OBJECT_0 == ret) {
                bytesReceived = recv(handle->iSocket, (char*)buffer, bufferBytes, 0);
            }
        }
        WSACloseEvent(evt);
        return bytesReceived;
    }
    

    Client code would call WSASetEvent on recvCancelEvt if it wanted to cancel a recv.

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

Sidebar

Related Questions

I'm writing a small Windows application in Visual C++ without MVC. Its really small,
I am writing Windows application (with Borland C++ Builder), which stores large number of
I'm writing a windows application in c#,FW3.5 & Visual Studio 2008. I need to
I have a small windows application I wrote using Visual Studio 2010, CSharp, .NET
I have a multithreading server application which runs fine for the first ca. 40
I am writing a Windows 8 Store app using Visual Studio 2012. The main
I'm writing a cross-platform code, which should compile at linux, windows, Mac OS. On
I'm using visual stduio 2008 c++. And I'm writing windows form application. I declare
I have problem in writing the code in windows form application my problem is
iam writing a c# windows application to store some data into a sql server

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.