简体   繁体   中英

Linux Socket re-reading old data

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM