I have a TCP server and client, with an established socket. Let\'s say I have the following case:
SERVER:
char *fn = \"John\";
char *ln = \"Doe\";
char
First, when the client calls recv(*socket, buffer, sizeof(buffer), 0);
, it will receive up to sizeof(buffer) characters (here, 512) from the network. This is from the network buffer, and has nothing to do with how many times send
has been called on the server. recv
does not look for \0
or any other character - it's a binary pull from the network buffer. It will read either the entire network buffer, or sizeof(buffer)
characters.
You need to check in the first recv
to see if it has both the first and last name and handle it appropriately.
You can do so either by having the server send the length of the strings as the first few bytes of a stream (a "header"), or by scanning the received data to see if there are two strings.
You really need to perform checks whenever you call recv
- what if the string is longer than 512 bytes? Then you need to call recv
multiple time sto get the string. And what if you get the first string, and only half of the second string? Usually these two cases only happen when dealing with larger amounts of data, but I've seen it happen in my own code before, so it's best to incorporate good checks on recv
even when dealing with small strings like this. If you practice good coding tactics early on you won't have as many headaches down the road.