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.
select(2)
system call, that allows you to specify a timeout (with usec resolution) – Luis Colorado