Why is my DeflateStream not receiving data correctly over TCP?
Asked Answered
D

3

6

I have a TcpClient class on a client and server setup on my local machine. I have been using the Network stream to facilitate communications back and forth between the 2 successfully.

Moving forward I am trying to implement compression in the communications. I've tried GZipStream and DeflateStream. I have decided to focus on DeflateStream. However, the connection is hanging without reading data now.

I have tried 4 different implementations that have all failed due to the Server side not reading the incoming data and the connection timing out. I will focus on the two implementations I have tried most recently and to my knowledge should work.

The client is broken down to this request: There are 2 separate implementations, one with streamwriter one without.

textToSend = ENQUIRY + START_OF_TEXT + textToSend + END_OF_TEXT;

// Send XML Request
byte[] request = Encoding.UTF8.GetBytes(textToSend);

using (DeflateStream streamOut = new DeflateStream(netStream, CompressionMode.Compress, true))
{
    //using (StreamWriter sw = new StreamWriter(streamOut))
    //{
    //    sw.Write(textToSend);
    //    sw.Flush();
    streamOut.Write(request, 0, request.Length);
    streamOut.Flush();
    //}
}

The server receives the request and I do
1.) a quick read of the first character then if it matches what I expect
2.) I continue reading the rest.

The first read works correctly and if I want to read the whole stream it is all there. However I only want to read the first character and evaluate it then continue in my LongReadStream method.

When I try to continue reading the stream there is no data to be read. I am guessing that the data is being lost during the first read but I'm not sure how to determine that. All this code works correctly when I use the normal NetworkStream.

Here is the server side code.

private void ProcessRequests()
{
    //  This method reads the first byte of data correctly and if I want to
    // I can read the entire request here.  However, I want to leave
    // all that data until I want it below in my LongReadStream method.
    if (QuickReadStream(_netStream, receiveBuffer, 1) != ENQUIRY)
    {
        // Invalid Request, close connection
        clientIsFinished = true;
        _client.Client.Disconnect(true);
        _client.Close();
        return;
    }



    while (!clientIsFinished)  // Keep reading text until client sends END_TRANSMISSION
    {
        // Inside this method there is no data and the connection times out waiting for data
        receiveText = LongReadStream(_netStream, _client);

        // Continue talking with Client...
    }
    _client.Client.Shutdown(SocketShutdown.Both);
    _client.Client.Disconnect(true);
    _client.Close();
}


private string LongReadStream(NetworkStream stream, TcpClient c)
{
    bool foundEOT = false;
    StringBuilder sbFullText = new StringBuilder();
    int readLength, totalBytesRead = 0;
    string currentReadText;
    c.ReceiveBufferSize = DEFAULT_BUFFERSIZE * 100;

    byte[] bigReadBuffer = new byte[c.ReceiveBufferSize];

    while (!foundEOT)
    {
        using (var decompressStream = new DeflateStream(stream, CompressionMode.Decompress, true))
        {
            //using (StreamReader sr = new StreamReader(decompressStream))
            //{
                //currentReadText = sr.ReadToEnd();
            //}
            readLength = decompressStream.Read(bigReadBuffer, 0, c.ReceiveBufferSize);
            currentReadText = Encoding.UTF8.GetString(bigReadBuffer, 0, readLength);
            totalBytesRead += readLength;
        }

        sbFullText.Append(currentReadText);

        if (currentReadText.EndsWith(END_OF_TEXT))
        {
            foundEOT = true;
            sbFullText.Length = sbFullText.Length - 1;
        }
        else
        {
            sbFullText.Append(currentReadText);
        }

        // Validate data code removed for simplicity


    }
    c.ReceiveBufferSize = DEFAULT_BUFFERSIZE;
    c.ReceiveTimeout = timeOutMilliseconds;
    return sbFullText.ToString();

}



private string QuickReadStream(NetworkStream stream, byte[] receiveBuffer, int receiveBufferSize)
{
    using (DeflateStream zippy = new DeflateStream(stream, CompressionMode.Decompress, true))
    {
        int bytesIn = zippy.Read(receiveBuffer, 0, receiveBufferSize);
        var returnValue = Encoding.UTF8.GetString(receiveBuffer, 0, bytesIn);
        return returnValue;
    }
}

EDIT NetworkStream has an underlying Socket property which has an Available property. MSDN says this about the available property.

Gets the amount of data that has been received from the network and is available to be read.

Before the call below Available is 77. After reading 1 byte the value is 0.

//receiveBufferSize = 1
int bytesIn = zippy.Read(receiveBuffer, 0, receiveBufferSize);

There doesn't seem to be any documentation about DeflateStream consuming the whole underlying stream and I don't know why it would do such a thing when there are explicit calls to be made to read specific numbers of bytes.

Does anyone know why this happens or if there is a way to preserve the underlying data for a future read? Based on this 'feature' and a previous article that I read stating a DeflateStream must be closed to finish sending (flush won't work) it seems DeflateStreams may be limited in their use for networking especially if one wishes to counter DOS attacks by testing incoming data before accepting a full stream.

Determined answered 17/2, 2016 at 15:7 Comment(3)
Just speculating, but perhaps the constructor for DeflateStream in QuickReadStream is detecting that NetworkStream is a forward-only read-only stream and reading the entire thing into zippy. You then read the first byte from zippy, set your returnValue and return. When zippy goes out of scope there is nothing to be read in the NetworkStream because it has already been read and disgarded.Bar
I've updated my question based on your seemingly correct comment. This is fundamentally problematic as noted in my question.Determined
DOS mitigation is typically done at the network appliance level, where the stream data can be monitored/analyzed as it comes through. From an application perspective, I'm not sure there is much you can do about detecting or mitigating such an attack. My only suggestion would be to read the stream in its entirety into a MemoryStream, test it, then discard if it is not right. That would not lessen the TCP traffic burden of an attack, but may prevent unnecessary processing/storage of trash data.Bar
D
1

Basically there are a few things wrong with the code I posted above. First is that when I read data I'm not doing anything to make sure the data is ALL being read in. As per microsoft documentation

The Read operation reads as much data as is available, up to the number of bytes specified by the size parameter.

In my case I was not making sure my reads would get all the data I expected.

This can be accomplished simply with this code.

byte[] data= new byte[packageSize];
    bytesRead = _netStream.Read(data, 0, packageSize);
    while (bytesRead < packageSize)
        bytesRead += _netStream.Read(data, bytesRead, packageSize - bytesRead);

On top of this problem I had a fundamental issue with using DeflateStream - namely I should not use DeflateStream to write to the underlying NetworkStream. The correct approach is to first use the DeflateStream to compress data into a ByteArray, then send that ByteArray using the NetworkStream directly.

Using this approach helped to correctly compress data over the network and property read the data on the other end.

You may point out that I must know the size of the data, and that is true. Every call has a 8 byte 'header' that includes the size of the compressed data and the size of the data when it is uncompressed. Although I think the second was utimately not needed.

The code for this is here. Note the variable compressedSize serves 2 purposes.

 int packageSize = streamIn.Read(sizeOfDataInBytes, 0, 4);
 while (packageSize!= 4)
 {
    packageSize+= streamIn.Read(sizeOfDataInBytes, packageSize, 4 - packageSize);
 }
 packageSize= BitConverter.ToInt32(sizeOfDataInBytes, 0);

With this information I can correctly use the code I showed you first to get the contents fully.

Once I have the full compressed byte array I can get the incoming data like so:

var output = new MemoryStream();
using (var stream = new MemoryStream(bufferIn))
{
    using (var decompress = new DeflateStream(stream, CompressionMode.Decompress))
    {
        decompress.CopyTo(output);;
    }
}
output.Position = 0;
var unCompressedArray = output.ToArray();
output.Close();
output.Dispose();
return Encoding.UTF8.GetString(unCompressedArray);
Determined answered 26/2, 2016 at 18:28 Comment(0)
R
3

The basic flaw I can think of looking at your code is a possible misunderstanding of how network stream and compression works.

I think your code might work, if you kept working with one DeflateStream. However, you use one in your quick read and then you create another one.

I will try to explain my reasoning on an example. Assume you have 8 bytes of original data to be sent over the network in a compressed way. Now let's assume for sake of an argument, that each and every byte (8 bits) of original data will be compressed to 6 bits in compressed form. Now let's see what your code does to this.

From the network stream, you can't read less than 1 byte. You can't take 1 bit only. You take 1 byte, 2 bytes, or any number of bytes, but not bits.

But if you want to receive just 1 byte of the original data, you need to read first whole byte of compressed data. However, there is only 6 bits of compressed data that represent the first byte of uncompressed data. The last 2 bits of the first byte are there for the second byte of original data.

Now if you cut the stream there, what is left is 5 bytes in the network stream that do not make any sense and can't be uncompressed.

The deflate algorithm is more complex than that and thus it makes perfect sense if it does not allow you to stop reading from the NetworkStream at one point and continue with new DeflateStream from the middle. There is a context of the decompression that must be present in order to decompress the data to their original form. Once you dispose the first DeflateStream in your quick read, this context is gone, you can't continue.

So, to resolve your issue, try to create only one DeflateStream and pass it to your functions, then dispose it.

Rules answered 17/2, 2016 at 16:25 Comment(3)
Well, a single deflate for the entire method seemed to help, but the sequence is still failing before the conversation is complete. I am thinking it is not possible to use DeflateStream with a tcpclient connection for continued back and forth communication.Determined
You can't go back in that stream, but you should be able to read a part of it and then read the rest of it. It would surprise me really, if this was not possible and you would have to read it all in once.Rules
@AdamHeeg oh right, you cannot reliably flush a deflate stream I think because the output might be at a partial byte position. You probably need to devise a message framing format where you prepend the compressed length as an uncompressed integer. Then, send the compressed deflate stream. This complicates things further of course. Will append this to my answer.Mocambique
M
2

This is broken in many ways.

  1. You are assuming that a read call will read the exact number of bytes you want. It might read everything in one byte chunks though.
  2. DeflateStream has an internal buffer. It can't be any other way: Input bytes do not correspond 1:1 to output bytes. There must be some internal buffering. You must use one such stream.
  3. Same issue with UTF-8: UTF-8 encoded strings cannot be split at byte boundaries. Sometimes, your Unicode data will be garbled.
  4. Don't touch ReceiveBufferSize, it does not help in any way.
  5. You cannot reliably flush a deflate stream, I think, because the output might be at a partial byte position. You probably should devise a message framing format in which you prepend the compressed length as an uncompressed integer. Then, send the compressed deflate stream after the length. This is decodable in a reliable way.

Fixing these issues is not easy.

Since you seem to control client and server you should discard all of this and not devise your own network protocol. Use a higher-level mechanism such as web services, HTTP, protobuf. Anything is better than what you have there.

Mocambique answered 17/2, 2016 at 16:51 Comment(1)
I appreciate your comments. We'll be doing our best to implement our own solution, using a higher level mechanism is not preferred here. I will try to use your advice to my advantage, thank you. I agree I have a lot to learn about this topic.Determined
D
1

Basically there are a few things wrong with the code I posted above. First is that when I read data I'm not doing anything to make sure the data is ALL being read in. As per microsoft documentation

The Read operation reads as much data as is available, up to the number of bytes specified by the size parameter.

In my case I was not making sure my reads would get all the data I expected.

This can be accomplished simply with this code.

byte[] data= new byte[packageSize];
    bytesRead = _netStream.Read(data, 0, packageSize);
    while (bytesRead < packageSize)
        bytesRead += _netStream.Read(data, bytesRead, packageSize - bytesRead);

On top of this problem I had a fundamental issue with using DeflateStream - namely I should not use DeflateStream to write to the underlying NetworkStream. The correct approach is to first use the DeflateStream to compress data into a ByteArray, then send that ByteArray using the NetworkStream directly.

Using this approach helped to correctly compress data over the network and property read the data on the other end.

You may point out that I must know the size of the data, and that is true. Every call has a 8 byte 'header' that includes the size of the compressed data and the size of the data when it is uncompressed. Although I think the second was utimately not needed.

The code for this is here. Note the variable compressedSize serves 2 purposes.

 int packageSize = streamIn.Read(sizeOfDataInBytes, 0, 4);
 while (packageSize!= 4)
 {
    packageSize+= streamIn.Read(sizeOfDataInBytes, packageSize, 4 - packageSize);
 }
 packageSize= BitConverter.ToInt32(sizeOfDataInBytes, 0);

With this information I can correctly use the code I showed you first to get the contents fully.

Once I have the full compressed byte array I can get the incoming data like so:

var output = new MemoryStream();
using (var stream = new MemoryStream(bufferIn))
{
    using (var decompress = new DeflateStream(stream, CompressionMode.Decompress))
    {
        decompress.CopyTo(output);;
    }
}
output.Position = 0;
var unCompressedArray = output.ToArray();
output.Close();
output.Dispose();
return Encoding.UTF8.GetString(unCompressedArray);
Determined answered 26/2, 2016 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.