You do some work, which takes x msec of real-time, some of which is spent blocked on some other I/O (so the CPU isn't busy, but it's not being used to read another packet).If the CPU is not 100%, then perhaps what's happening is: If the CPU is 100% utilitized then you need to do less work per packet or get a faster CPU (or use multithreading and more CPUs if you aren't already). Otherwise you need to see why you're not reading fast/often enough. One option is to change the network protocol: use TCP, or implement some acknowledgement + 'flow control' using UDP. If so, what are the options to correct this? Packets can be dropped if they're arriving faster than you read them. Is it possible that reading this buffer will cause incoming packets to be dropped? If you can successfully do this without dropping packets, then breaking your functionality into a "receiving" thread and a "processing" thread will help.
It then waits to be signaled again.Īs a test, try short-circuiting the full processing of your packets and just write a message to the console (or a file) each time a packet has been received.
It then pulls the packet from the properly-synchronized shared data structure and processes it. It sits idle until the first thread signals a packet has been received. The second thread exists to process the received packets. In other words, the thread's sole purpose is to read a packet from the socket, add it to some kind of properly-synchronized shared data structure, signal that a packet has been received, and then read the next packet. The first thread exists solely to service the socket. Typically, you want two threads for this kind of situation.
Use the setsockopt Windows function to increase the size of the buffer (refer to the SO_RCVBUF option).īut beyond that, increasing the size of your receive buffer will only delay the time until packets get dropped again if you are not reading the packets fast enough.