So, I have this piece of code, which will just read the message from the client and reply with a “yup”
while(strcmp(buffer, "QUIT") != 0){
bzero(buffer, 255); //cleans the read buffer
/*this reads the aux (request) from the client*/
recv(newsockfd, buffer, 255, 0);
if(strlen(buffer))
printf("Thread %d: %s\n", thread_no, buffer);
fflush(stdout);
write(newsockfd, "yup\n", 4);
}
The problem is that at the very first reading everything goes ok, but all other readings are messed up, if I send the message “guitar”, for example, it gets the ‘g’, loops and then it gets the “uitar”, sending aother “yup”.
I have no clue what’s happening.
Long story short: TCP isn’t a message orientated protocol, it’s a stream orientated protocol. Messages might be fragmented or merged together, and your application has to deal with that (the only guarantee is that you’ll receive the data in the same order you sent it in).