Question

I am setting UDP receive timeout option using something like this:

    struct timeval t;
    t.tv_sec = 0;
    t.tv_usec = 5;

    if(setsockopt(destination_fd, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t)) == -1){
        perror("Setting SO_RCVTIMEO option in UDP socket for destination RX: ");
        print_error_and_exit("Couldn't set SO_RCVTIMEO option in UDP socket for destination RX");
    }

I want to set UDP receive timeout to 5 microseconds, but this is not working.

When there are no packets receive, UDP is taking around at least 4 milliseconds to timeout even if I have set 5 microseconds.

How can I make to timeout in 5 microseconds if no packets available.

I guess may be process going to blocked state, and scheduler schedules other process, and so always it takes around 4 milliseconds. If so, how can I make call to recvfrom() to receive UDP packet to fail without blocking if there is no data to receive?

Was it helpful?

Solution

5 microsecond is a super small duration, you need more time just to switch to the kernel to process your recv system call. 4 ms though sounds a bit long to me.

The fastest you can do is set no timeout at all, use select to check if the socket have a pending packet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top