2
votes

I have to manage multiple timers for a UDP file transfer application, after a timeout the server had to resend packets to the client, but there are more than one packet a time that could cause the timeout.

So I have to manage a timer for each packet. How can I do this?

I can't use alarm because it cancelled the previous timers and also works only with seconds.

1
Use select(2) system call, that allows you to specify a timeout (with usec resolution)Luis Colorado

1 Answers

1
votes

You need to keep an array of structs containing timeouts for each packet you want to keep track of.

Each array element should contain the starting time and expected ending time for each timeout. When it's time to set the timer, check all entries in the array to see which one is expected to time out first. Then subtract that time from the current time to get your timeout value for select.

When the socket read times out, go through the list again and for each packet whose timeout time is prior to the current time, handle the timeout for that packet.

Take a look at the source of a multicast file transfer application I wrote called UFTP for an example of how this can be implemented. Specifically, look at the getrecenttimeout function in client_loop.c.