Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCP Streaming/Segmentation issue #1

Open
ademclk opened this issue Dec 18, 2023 · 1 comment
Open

TCP Streaming/Segmentation issue #1

ademclk opened this issue Dec 18, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@ademclk
Copy link
Owner

ademclk commented Dec 18, 2023

There’s a problem that when you send multiple messages to the client, they receive it as a single message.

This is a common issue with TCP, which is a stream-based protocol and does not guarantee that messages will arrive as sent. Instead, TCP guarantees that all bytes will arrive in the order they were sent.

This means that if we send two messages, "Hello" and "World", it's possible that the client will receive them as a single message, "HelloWorld". It causes an error while checking parity bits.

To handle this, implement a message framing protocol on top of TCP. One common method is to prefix each message with its length.

@ademclk ademclk added the bug Something isn't working label Dec 18, 2023
@ademclk ademclk self-assigned this Dec 18, 2023
@ademclk
Copy link
Owner Author

ademclk commented Dec 19, 2023

In TCP, data is sent as a continuous stream of bytes. As such, it does not inherently preserve message boundaries. If you send two messages "Hello" and "World", TCP could deliver them as one combined message "HelloWorld". This can cause issues when trying to interpret the received data, as seen in the issue with checking parity bits.

The solution to this problem is to implement a message framing protocol on top of TCP. In this case, we've used a length-prefix framing where each message is prefixed with its length. This means that before sending a message, we first send the length of the message, allowing the receiver to know exactly how many bytes to expect for each complete message.

The implementation involves modifying the send and receive functions. In the sendDelimitedMessage() function, we first send the length of the message (converted to network byte order), then the actual message. On the receiver side, in the receiveDelimitedMessage() function, we first read the length of the message, then read that many bytes for the actual message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant