Reputation: 3
I am writing a Linux socket program and when I read data from the socket I have old data values from previous reads in my buffer. I am using the read() call but I have also tried recv() and both result in the same frustrating error. Here are the three read calls:
read(client_sock, &test, sizeof(test));
read(client_sock, &test2, sizeof(test2));
read(client_sock, &test3, sizeof(test3));
test, test2, and test3 are all char and are attempting to read in char values written in by a client. Any help would be greatly appreciated.
Upvotes: 0
Views: 516
Reputation: 121599
I suspect the problem is you're assuming that your read succeeded, and that you read as much data as you asked for.
This will NOT necessarily be the case!
You totally, absolutely, positively need to get the RETURN VALUE from "read()". It might be LESS than your buffer, it might be "-1" (an error).
Upvotes: 5