I am fairly new to C and writing a TCP server, and was wondering how to handle recv()
s from a client who will send commands that the server will respond to. For the sake of this question, let's just say header is 1st byte, command identifier is 2nd byte, and payload length is 3rd byte, followed by the payload (if any).
What is the best way to recv()
this data? I was thinking to call recv()
to read in the first 3 bytes into the buffer, check to make sure header and command identifiers are valid, then check payload length and call recv()
again with payload length as length and add this to the back of the aforementioned buffer. Reading Beej's networking article (particularly the section Son of Data Encapsulation)), however, he advises to use "an array big enough for two [max length] packets" to handle situations such as getting some of the next packet.
What is the best way to handle these types of recv()
s? Basic question, but I would like to implement it efficiently, handling all cases that can arise. Thanks in advance.