I'm using the setsockopt()
function to set a timeout on the recvfrom()
function. Because of the protocol I am using, I have to first have a timeout of 2 secs, then 4, 6, until a max. But when I use the function, it seems to have a 0.01 seconds timeout because it sends 8 packets without waiting.
//more variables and code here
struct timeval timeout = {2,0};
while(1){
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(struct timeval));
temp2 = recvfrom(sock, &buff, sizeof(buff), 0, (struct sockaddr *)&addr_server, sizeof(addr_server));
if(temp2 < 0){ /* Timeout excedit (exceeded)*/
temp = sendto(sock, (struct udp_PDU*)®_pdu, sizeof(reg_pdu), 0, (struct sockaddr *)&addr_server, sizeof(addr_server));
if(temp == -1){
printf("Error sendTo \n");
exit(-1);
}
packet_counter++;
debug("Enviat paquet REGISTER_REQ");
if(packet_counter == 8) break;
if((interval * max) > t ) timeout.tv_sec+=interval;
}else{ /* s'han rebut dades (they have rebooted) */
correct = 1;
break;
}
}