I build a simple application using c that used recvmmsg()
, and the fifth parameter passed is timeout of type struct timespec
. I set timeout to 5 seconds, but it's not working, it gets blocking infinity.
The code is as the following:
struct timespec timeout;
timeout.tv_sec = 5;
timeout.tv_nsec = 0;
result = recvmmsg(fd, datagrams, BATCH_SIZE, 0, &timeout);
As an alternative, you could use setsockopt
with SO_RCVTIMEO
option to set a timeout on the socket. This will affect all read operations performed on it.
See here: http://permalink.gmane.org/gmane.linux.man/3440
Basically the timeout parameter specifies a maximum amount of time to wait for more messages, but the underlying receive operation is still blocking. So if you set a timeout of 5 seconds and receive one message every second, it will stop after receiving (about) 5 messages even if there is space in the buffers for more. What it will not do is return after 5 seconds if there is no data coming at all. For that you should use one of the usual mechanisms, like select() or epoll() with a timeout, or busy waiting, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With