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;
}