Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send UDP packets at a specified rate in C++ on Windows?

I'm writing a program that implements the RFC 2544 network test. As the part of the test, I must send UDP packets at a specified rate.

For example, I should send 64 byte packets at 1Gb/s. That means that I should send UDP packet every 0.5 microseconds. Pseudocode can look like "Sending UDP packets at a specified rate":

while (true) {
    some_sleep (0.5);
    Send_UDP();
}

But I'm afraid there is no some_sleep() function in Windows, and Linux too, that can give me 0.5 microseconds resolution.

Is it possible to do this task in C++, and if yes, what is the right way to do it?

like image 636
usamytch Avatar asked Dec 05 '25 22:12

usamytch


1 Answers

Two approaches:

  • Implement your own sleep by busy-looping using a high-resolution timer such as windows QueryPerformanceCounter

  • Allow slight variations in rate, insert Sleep(1) when you're enough ahead of the calculated rate. Use timeBeginPeriod to get 1ms resolution.

For both approaches, you can't rely on the sleeps being exact. You will need to keep totals counters and adjust the sleep period as you get ahead/behind.

like image 67
Erik Avatar answered Dec 08 '25 12:12

Erik