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?
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:
then use WSAWaitForMultipleEvents to wait until either data arrives or you want to cancel the recv:
Client code would call
WSASetEvent
onrecvCancelEvt
if it wanted to cancel a recv.