c# - Index out of range at "int msgLength = BitConverter.ToInt32(gzBuffer, 0);" -
I am currently working on some games created in C # XNA. Since I need to send a large part to Net (data 96kb), I'm using some string compressor / decompressor code, which I found on the Internet. The code looks like this:
public static string short (string text) {byte [buffer = encoding.utf 8.gatebyte (text); Memorystream ms = new memorystream (); (Gzipstream zip = new gzipstream (ms, compression mode, compress, true) use (zip) .vit (buffer, 0, buffer lang);} Ms.osition = 0; memorystream outstream = new memorystream (); byte [] Compressed = new byte [ms.lamp]; ms.Read (compressed, 0; compressed lang); byte [] gzBuffer = new byte [compressed. Length = 4]; System.Buffer.BlockCopy (compressed, 0, gzBuffer, 4, Compressed.); System.Buffer.BlockCopy (BitConverter.GetBytes (Buffer Lange), 0, JizzBuffer, 0, 4); Return Convert. Tubes 64 Stri C (jizbuffer);} Public String Decompress (String Compressed Text) {Byte [gzBuffer = Convert.FromBase64String (compressedText); (MemoryStream MS = New MemoryStream ()) Using {int msgLength = BitConverter.ToInt32 (gzBuffer, 0); Ms.Write (gzBuffer, 4, gzBuffer.Length - 4); byte [] buffer = new byte [msgLength]; ms.osition = 0; (gzipstream zip = new gzipstream (ms, compression mod.decampres)) Use (zip. Read (buffer, 0, buffer lang);} return encoding. UTF8.GetString (buffer);}}
The works: - Server string compresses the string using the compressor, where after sending the client. - Receives compressed strings to the client, and decompresses it, so it is usable.
Now weird part, this code works, when I test the game alone, which means that I run the server and the client, the same machine works even when I I'm testing with a friend, where he hosts the server, and I connect. But This is not working for my friend when I am hosting the server and he connects when he connects to his server, he receives the "index out of range", "IntelMELL = Bitkonver. Two-eyed 32 (JESbuffer, 0); " Sometimes he is lucky and goes in, but mostly fails in it and returns returns error. Could it be that the server is not sending the entire string? I am using Lidgren Gen3 to send the network, and the compressed string is sent as netMsg.Write (compressedstring);
Any ideas :)? Thanks!
You are creating the same misconception in many places: that stream. Read
will return all the data you asked to go once you are ignoring the exchange value. Do not do this ... as if it should be ok for MemoryStream
s, assuming that there is enough data, besides that, you can use the MemoryStream.ToArray
method A byte array for
Now, you have not shown how the client is getting the string - where are you getting it?
You should log the string that the server sends, and the string that the client receives is the complete first step, so you can see whether the data is actually protected or not.
I suspect that the client is trying to decompress the empty string - that is, it is returning a Convert.FromBase64
- an empty byte array - but as long as you do that string Do not know who is being received, anybody is hard to make progress.
Comments
Post a Comment