I need to transmit full byte packets in my custom format via TCP. But if I understand correctly TCP is streaming protocol, so when I will call send
method on sender side, there is not guaranty that it will be received with same size on receiver side when call recv
(It can be merged together with Nagle's algorithm and then splited when will not fit into frame or when not fit to buffer).
UDP provides full datagrams so there is no such issue.
So question is: what will be the best and correct way to recv
same pacakges as it was send
, with same size, with no glue. I develop using python.
I think I can use something like HDLC
but I am not sure that iterating throug each byte will be best choice.
Maybe there are some open-source small examples for this situation or it is discribed in books?
Since TCP is only an octet stream this is not possible without glue, either around your data (i.e. framing) or inside your data (structure with clear end).
The way this is typically done is either by having a delimiter (like \r\n\r\n
between HTTP header and body) or just prefix your message with the size. In the latter case just read the size (fixed number of bytes) and then read this number of bytes for the actual message.