1

I have a server and a client connecting using TCP socket. At the client (which is written in C++) I compress a string (str) using the zlib library:

uLongf compressedStringLength = compressBound(str.length());
Bytef* compressedString = new Bytef[compressedStringLength];
const Bytef* originalString = reinterpret_cast<const Bytef*>(str.c_str());
compress(compressedString, &compressedStringLength, originalString, str.length());

Then I send it to the server. This is the line of code where I write the compressed string to the socket:

int bytesWritten = write(sockfd, &compressedString, compressedStringLength);

At the server (which is written in C#) I recieve the stream of the compressed bytes from the socket and decompress it using the DeflateStream class:

NetworkStream stream = getStream(ref server); //server is a TcpListener object which is initialized to null.
byte[] bytes = new byte[size];
stream.Read(bytes, 0, size);
Stream decompressedStream = Decompress(bytes);

This is the Decompress function:

private static Stream Decompress(byte[] input)
{
        var output = new MemoryStream();

        using (var compressStream = new MemoryStream(input))
        using (var decompressor = new DeflateStream(compressStream, CompressionMode.Decompress))
        {
            decompressor.CopyTo(output);
        }

        output.Position = 0;
        return output;
 }

The compressing process and sending the compressed bytes over the socket works, but I get an exception at the line decompressor.CopyTo(output); at the Decompress function above: System.IO.InvalidDataException: Block length does not match with its complement.

Does someone know what the problem is and how can I solve it?

EDIT: I`ve already tried to skip the first two bytes brfore the start of the decompression process.

user382704
  • 19
  • 4
  • Have you seen these two SO posts? http://stackoverflow.com/questions/18540125/deflatestream-doesnt-work-on-memorystream, http://stackoverflow.com/questions/19165884/deflatestream-copyto-writes-nothing-and-throws-no-exceptions They may help at least with some idea of how to do this. – pookie Nov 08 '15 at 13:20
  • Where does `size` come from? – Alan Stokes Nov 08 '15 at 13:30
  • 1
    Could be an endianness error. – Puppy Nov 08 '15 at 14:48
  • `size` is the size in bytes of the decompressed string. I send it from the client along with the compressed string. – user382704 Nov 08 '15 at 14:49
  • 1
    You're using it as if it was the size of the compressed string. – Alan Stokes Nov 08 '15 at 16:12
  • See also [.Net zlib inflate with .Net 4.5](https://stackoverflow.com/q/17212964), [Cannot decompress ZLIB/DEFLATE data](https://stackoverflow.com/q/6200395), [Decoding git objects / “Block length does not match with its complement” error](https://stackoverflow.com/q/8354811), [Good zlib implementation in .NET?](https://stackoverflow.com/q/513234), and [zlib compressing byte array?](http://stackoverflow.com/q/6282575) (to name a few of the many other related posts on Stack Overflow) – Peter Duniho Nov 08 '15 at 19:53
  • I`ve managed to **solve this issue**. The problem was that at the line of code where I write the compressed string to the socket: `int bytesWritten = write(sockfd, &compressedString, compressedStringLength);` instead of writing the compressed data itself I write the address of `compressedString` which is define as a pointer: `Bytef* compressedString = new Bytef[compressedStringLength];` Many thanks to all of you anyway. – user382704 Nov 09 '15 at 09:49

0 Answers0