3

I am trying to implement the the timeout mechanism in my c implementation of TFTP, and i am looking for some general help.

What I am wondering is how to manage the timeout situation. The premature timeout mechanism that I used is with signal/alarm functions, but somehow I am stuck in how to handle my timeouts, that is if the packet (ack or data) is missed and a timeout occurs how to send back the previous packet or ack to the server.

EasyQuestions
  • 327
  • 1
  • 10
  • 23

1 Answers1

5

Avoid signal and alarm if possible.

Either use SO_RCVTIMEO socket option or just use select with a timeout of T seconds.

If the select() call returns and your socket is not in the read set, or if recvfrom returns with a timeout error, then you can take appropriately action in your code.

Example of timeout usage:

timeval tv = {0,0};
tv.tv_sec = 5;
socklen_t optionlength = sizeof(tv);
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, optionlength);

while (1)
{
    result = recvfrom(s, buffer, bufferlength, 0);
    if (result == -1) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)) )
    {
       // handle timeout
    }
    else if (result == -1)
    {
       // handle critical error
    }
    else
    {
       // process next packet
    }
}

Example of select usage:

while (1)
{
    timeval tv = {0,0};
    tv.tv_sec = 5;
    fd_set readset = {};
    FD_ZERO(&readset);
    FD_SET(s, &readset);

    select(s+1, &readset, NULL, NULL, &tv);

    if (FD_ISSET(s, &readset))
    {
        result = recvfrom(s, buffer, bufferlength, 0);
        if (result == -1)
        {
            // handle error
        }
        else
        {
            // process packet
        }
    }
    else
    {
       // handle timeout
    }

}
user3490561
  • 446
  • 3
  • 8
  • 20
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Is there some examples that I can get an idea. form Also I was wondering if I am sending a in a one file, and then receive it in a server implementation file, How can I manage that, since the timeout have different scope in each file. – EasyQuestions May 11 '13 at 21:29
  • @EasyQuestions - everything you need to know is here: http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html – selbie May 12 '13 at 02:25
  • Thanks, I read that before, which is an awesome piece of article but he doesn't talk much about the timeout situations. – EasyQuestions May 12 '13 at 03:27
  • select and timeout are essentially equivalent. I've posted examples above for both. The advantage of select (or poll) is that you can have a server processing multiple sockets at once. – selbie May 12 '13 at 04:58
  • Oh thanks, it makes much more sense now, however I edited my original post to better state my problem. If you could take a look at it, that'll be great. – EasyQuestions May 12 '13 at 06:22
  • I do not understand why you want to have a timeout. Explain that first, and I'll explain what you should do next. – selbie May 12 '13 at 07:23
  • This TFTP is implemented on top of UDP, hence there is no guaranty for reliable transmission of packets. The timeout mechanism allows that if an ack/data packet doesn't reach the destination. The packet is sent again, hence we can make sure that all the packets eventually arrive to destination. That's why I want a timeout. By the way thanks very much, I gave up the idea of alarm and signal, and am trying to make the SO_RCVTIMEO work. Your input is appreciated. – EasyQuestions May 12 '13 at 08:45
  • Great, and welcome to stack overflow. So click the little green checkbox if you like the answer. – selbie May 12 '13 at 08:53
  • I might bug you again though. – EasyQuestions May 12 '13 at 09:22