Original Post
Hi, Im a newbie in network programming. AFAIK, in a TCP connection you cant assure that a sent message will arrive in the same packet, I mean, all what's sent int a send() may be split across different recv(). I now have another question, can the information sent across 2 different send() be merged by the kernel into the same recv()? Now I have to create a way to send/receive full packets. I have thought of sending at first the size of the packet, and then read the packet. For example, in if using an event-driven socket library, I might have something like (in C++-pseudocode mix), supposed that tho send() cant't be merged in the same read(): Is this the right way of doing it? I imagine that I would have to make some kind of time-out check in case the connection drops while reading a packet.
class packetreader : public tcpsocket {
..
handleRead() {
if (packet_size == 0) { /* We are not reading anithing */
socket_buffer >> packet_size;
}
buffer += read();
if (buffer.size() == packet_size) {
handlePacket(buffer);
buffer.clear();
packet_size = 0;
}
}
..
private:
packet_size;
buffer;
}