Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timeout in recvmmsg()?

Tags:

c

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);
like image 250
Amjad Omari Avatar asked May 02 '13 14:05

Amjad Omari


2 Answers

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.

like image 174
R.. GitHub STOP HELPING ICE Avatar answered Sep 21 '22 16:09

R.. GitHub STOP HELPING ICE


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.

like image 37
John Zwinck Avatar answered Sep 23 '22 16:09

John Zwinck