Reputation: 2158
I am writing a protocol to send files over the TCP layer. How are commands usually separated from payloads? I know in advance how big the payload will be, should I send the length beforehand?
I am also thinking about just using HTTP to transfer the files, any opinions on whether this will be less time consuming? I am using .NET so the underlying protocols are working well already.
Upvotes: 1
Views: 87
Reputation: 73294
How are commands usually separated from payloads? I know in advance how big the payload will be, should I send the length beforehand?
Yes. A typical pattern would be to send a fixed-length header (e.g. number-of-bytes, in big-endian integer format), followed by the actual bytes of data (and optionally, repeat as necessary). Be sure to use a fixed-width type for your header (e.g uint64_t rather than unsigned long), since you'd (presumably) like your protocol to work the same regardless of what machine it is compiled on.
I am also thinking about just using HTTP to transfer the files, any opinions on whether this will be less time consuming? I am using .NET so the underlying protocols are working well already.
Either way will work fine. Use whatever you're most comfortable with.
Upvotes: 1
Reputation: 171246
Use a higher-level protocol. That will save you vast amounts of time. HTTP sounds like a perfect fit because you seem to have a request-response pattern.
Writing TCP protocols is hard. The best way to get it right is: don't do it.
If HTTP does not work for you, investigate protocol buffers.
Upvotes: 1