0

I am trying to set a timeout for a UDP socket. On the server side, I simulate a connection failure by withholding the response struct about 1/3 of the time. My code works fine with no data loss but I cannot figure out how to make the client resume when the server does not respond. Here is the code:

struct request
{
    char clientIP[16];  /* Holds client IP address in dotted decimal */
    char m[24];         /* Name of machine on which client is running */
    int c;              /* Client number */
    int r;              /* Request number of client */
    int i;              /* Incarnation number of client’s machine */
    char operation[80]; /* File operation client sends to server */
};

struct returnValue
{
    int code;
    int bytes;
    char data[80];
};

void sendRequest(request myRequest)
{
    /* Send each request struct to the server */
    if((sendto(sock, &myRequest, sizeof(struct request), 0, (struct sockaddr *)
               &servAddr, sizeof(servAddr)) != sizeof(struct request)))
    {
        DieWithError("sendto() sent a different number of bytes than expected.");
    }
}

returnValue responseQuery(returnValue myReturn) /* Recv a response from server*/
{
    fromSize = sizeof(fromAddr);
    if ((respReqLen = recvfrom(sock, &myReturn, sizeof(struct returnValue), 0, (struct sockaddr *) &fromAddr, &fromSize)) != sizeof(struct returnValue))
    {
        DieWithError("recvfrom() failed");
    }
    if (servAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr)
    {
        fprintf(stderr,"Error: received a packet from unknown source.\n");
        exit(1);
    }
    return myReturn;
}
Johnny
  • 675
  • 3
  • 15
  • 25
  • Have you considered using [`select`](http://www.on-time.com/rtos-32-docs/rtip-32/reference-manual/socket-api/select.htm)? – Captain Obvlious Sep 21 '14 at 22:56
  • already answered: http://stackoverflow.com/questions/13547721/udp-socket-set-timeout – Andrius Bentkus Sep 21 '14 at 22:57
  • @AndriusBentkus I saw this answer but I cannot make sense of it. How would I implement it with my code here? – Johnny Sep 21 '14 at 23:04
  • If you can't make sense of that, you won't make sense of any other correct answer, as they are all the same. The answer contains the code you need. Just adjust the variable names and timeou values for your case. – user207421 Sep 22 '14 at 00:43

0 Answers0