0

I have a program that work like a chat.
Client and server are connected with 2 TCP sockets, one for incoming messages another for outgoing messages.
Sometimes the messages can be very big (ex. 2 MByte of text) so I want to compress them before sending over the channel.
The problem is that I don't know how to find the start and end of compressed message.
Now I use two special characters to find start and end of message but with compression there can be errors.

There is maybe a type of compression that don't use some specific bytes?
I use C# to open and manage sockets so I need a compression that work under windows.

2
  • 3
    Append to start of message it length. After that you just need to read length, and after that get exactly count of bytes what you need. Commented Aug 23, 2014 at 17:52
  • @Vlad you should post that as an answer. Commented Aug 23, 2014 at 20:46

2 Answers 2

1

Append to start of message it length. After that you just need to read length, and after that get exactly count of bytes what you need.
It will looks like:

|length|data|..|..|length|data|..|..|..|

And more exactly

|3|26|125|36|4|12|45|16|34|

Where 3 and 4 are length.

Sign up to request clarification or add additional context in comments.

Comments

0

You just need an escaping scheme.

  • Send STX (Start of Transmission) at the start.
  • Send ETX (End of Transmission) at the end.
  • If an STX or ETX appears in the data, prefix it with ESC (escape).
  • If an ESC appears in the data, prefix it with ESC.

At the receiver:

  • The first byte should be STX, otherwise you have a bug. Discard it.
  • After that, if a byte is ESC, discard it and accept the next byte, whatever it is.
  • Otherwise, if the next byte is ETX, discard it and stop reading.

The problem with the length-word prefix suggested in another answer is that you can't know the length without doing the compression first, which costs time and space.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.