C # socket message compression

I am new to C # world. I use it to quickly deploy a live-feed capture solution that comes in this form (braces for clarity): {abcde} {CompressedMessage}, where {abcde} is 5 characters that indicate the length of the compressed message. CompressedMessage is compressed using XCeedZip.dll and needs to be compressed without using the uncompress dll method. The uncompress method returns an integer value indicating success or failure (various kinds, for example, lack of a license, refusal to compress, etc.). I get a 1003 failure http://doc.xceedsoft.com/products/XceedZip/ for referring to return values ​​from uncompress method.

while(true){ byte[] receiveByte = new byte[1000]; sock.Receive(receiveByte); string strData =System.Text.Encoding.ASCII.GetString(receiveByte,0,receiveByte.Length); string cMesLen = strData.Substring(0,5); // length of compressed message; string compressedMessageStr = strData.Substring(5,strData.Length-5); byte[] compressedBytes = System.Text.Encoding.ASCII.GetBytes(compressedMessageStr); //instantiating xceedcompression object XceedZipLib.XceedCompression obXCC = new XceedZipLib.XceedCompression(); obXCC.License("blah"); // uncompress method reference http://doc.xceedsoft.com/products/XceedZip/ // visual studio displays Uncompress method signature as Uncompress(ref object vaSource, out object vaUncompressed, bool bEndOfData) object oDest; object oSource = (object)compressedBytes; int status = (int) obXCC.Uncompress(ref oSource, out oDest, true); Console.WriteLine(status); /// prints 1003 http://doc.xceedsoft.com/products/XceedZip/ } 

So basically my question boils down to calling the uncompress method and the correct way to pass parameters. I am on unfamiliar territory in the .net world, so I won’t be surprised if the question is really simplified.

Thanks for answers..

########################################### updated

Now I do the following:

 int iter = 1; int bufSize = 1024; byte[] receiveByte = new byte[bufSize]; while (true){ sock.Receive(receiveByte); //fetch compressed message length; int cMesLen = Convert.ToInt32(System.Text.Encoding.ASCII.GetString(receiveByte,0,5)); byte[] cMessageByte = new byte[cMesLen]; if (i==1){ if (cMesLen < bufSize){ for (int i = 5; i < 5+cMesLen; ++i){ cMessageByte[i-5] = b[i]; } } } XceedZipLib.XceedCompression obXCC = new XceedZipLib.XceedCompression(); obXCC.License("blah"); object oDest; object oSource = (object) cMessageByte; int status = (int) obXCC.Uncompress(ref oSource, out oDest, true); if (iter==1){ byte[] testByte = objectToByteArray(oDest); Console.WriteLine(System.Text.Encoding.ASCII.GetString(testByte,0,testByte.Length)); } } private byte[] objectToByteArray(Object obj){ if (obj==null){ return null; } BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms,obj); return ms.ToArray(); } 

The problem is that the testByte script command produces gibberish. Any suggestions on how to move on? uncompress status variable is good and equal to 0.

+4
source share
1 answer

The first error does not always look at the return value of Receive ; you can’t imagine how much data you just read, and whether this is a whole message.

It seems to me that you damaged the message payload by processing all the data as ASCII. Instead of GetString for the entire buffer, you should use GetString with only 5 bytes .

The correct process:

  • continue calling Receive (data buffering or increasing the offset and decreasing the amount) until you have at least 5 bytes
  • process these 5 bytes to get the payload length
  • Continue the Receive call (buffering data or increasing offsets and decreasing numbers) until you have at least the payload length
  • handle the payload without converting to / from ASCII
+3
source

All Articles