sockets - C - SIGALRM not received -
i'm new signals, i'm trying set sigalrm on udp echo service, socket programming practice.
here have udp socket, client sends string server , waits response (any response, here string echoed server).
goals set sigalrm , let client resend string few times if no responses made server or udp packets lost.
here, used small sample , simplified long lines ..., can more details on github repo (line 51)
sigalrm-client.c
unsigned int tries = 0; void catchalarm() { tries += 1; } int main(int argc, char **argv) { // skipped // ... struct sigaction handler; handler.sa_handler = catchalarm; handler.sa_flags = 0; if(sigfillset(&handler.sa_mask) < 0) return 1; if(sigaction(sigalrm, &handler, 0) < 0) return 2; ssize_t bytes; bytes = sendto(servsock,...); while((bytes = recvfrom(servsock,...)) < 0) { // alarm went off if(errno == eintr) { // try 5 times if(tries < 5) { bytes = sendto(servsock,...); } else { fprintf(stdout, "no response, waiting...\n"); } } else { fprintf(stdout, "failed data\n"); return 3; } } // recvfrom() got something, cancel timeout alarm(0); fprintf(stdout, "received %d bytes of data\n", bytes); close(servsock); }
when run client, won't receive sigalrm signal , udp packets lost in first attempt?!
client won't retry sending string exit after 5 attempts, instead, waits server response forever!
prevents client sigalrm?
did miss here?
your code in github repo never calls alarm()
non-zero number. you'll never alarm signal delivered automatically unless request one. relying on other process send process alarm signal isn't resilient.
Comments
Post a Comment