I'm writing a client-server application. Attached is its simplified version which narrows down my issue. The idea is, that server sends the sturcture of 3 ints. Client receives the structure and based on the values, performs certain actions. This is performed in a loop. Here's a fragment from test_client.c:
Code:
numRead = fread(&array, sizeof(struct three_ints), 1, sock_fp);
if (numRead <=0)
printf("numRead is now %d\n", numRead);
sleep(2);
if (array.one == 0 && array.two == 0 && array.three == 0)
{
printf("Read zeros. Their current values are %d, %d, %d\n", array.one, array.two, array.three);
printf("About to read integers and char. Their current values are %d, %d, %c\n", other_array.intOne, other_array.intTwo, other_array.charOne);
fread(&other_array, sizeof(struct two_ints_char), 1, sock_fp);
printf("Read the integers and char. Their current values are %d, %d, %c\n\n", other_array.intOne, other_array.intTwo, other_array.charOne);
sleep(4);
}
else if ( array.one == -5 && array.two == -5 && array.three == -5 )
{
printf("I am at -5\n");
}
else if ( array.one == -10 && array.two == -10 && array.three == -10 )
{
printf("I am at -10\n");
}
else
{
printf("Read non-zeros. Their current values are %d, %d, %d\n", array.one, array.two, array.three);
printf("About to send some_int to server %d\n", ++some_int);
fwrite(&some_int, sizeof(int), 1, sock_fp);
printf("some_int sent\n\n");
sleep(6);
}
This version works fine. However, once I replace "else if" with just "if" (until recent I believed the outcomes are the same), the following function fails:
numRead = fread(&array, sizeof(struct three_ints), 1, sock_fp);
and numread contains zero value. It took me 2 days to figure it out, but why does it happen and how can "if" statement affect fread function I still have no idea. Can someone explain???
Thanks