문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top