Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 860487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:49:31+00:00 2026-05-15T08:49:31+00:00

I am currently working on some game made in C# XNA. Since I need

  • 0

I am currently working on some game made in C# XNA.
Since I need to send a huge chunk of data over net (bout 96kb), I am using some string compressor/decompressor code, which I found on the internet.
The code looks like this:

        public static string Compress(string text)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(text);
        MemoryStream ms = new MemoryStream();
        using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
        {
            zip.Write(buffer, 0, buffer.Length);
        }

        ms.Position = 0;
        MemoryStream outStream = new MemoryStream();

        byte[] compressed = new byte[ms.Length];
        ms.Read(compressed, 0, compressed.Length);

        byte[] gzBuffer = new byte[compressed.Length + 4];
        System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
        System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);

        return Convert.ToBase64String(gzBuffer);
    }

    public static string Decompress(string compressedText)
    {
        byte[] gzBuffer = Convert.FromBase64String(compressedText);
        using (MemoryStream ms = new MemoryStream())
        {
            int msgLength = BitConverter.ToInt32(gzBuffer, 0);
            ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

            byte[] buffer = new byte[msgLength];

            ms.Position = 0;
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
            {
                zip.Read(buffer, 0, buffer.Length);
            }

            return Encoding.UTF8.GetString(buffer);
        }
    }

It works like this:
– Server compresses the string using string compressor, where after sending it to the client.
– The client receives the compressed string, and decompress it so it is usable.

Now the weird part is, that the code works when I am testing the game alone, which means I run the server and client on the same machine.
It also works when I am testing with my friend, where he hosts the server, and I connect.
But it ain’t working for my friend when I am hosting the server and he connects.
When he connects to my server, he receives “Index out of range” at “int msgLength = BitConverter.ToInt32(gzBuffer, 0);”.

Sometimes he is lucky and gets in, but mostly it fails and returns that error.
Can it be that the server ain’t getting the whole string sent?
I am using Lidgren gen3 for network sending, and the compressed string is sent as netMsg.Write(compressedstring);

Any ideas 🙂 ?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:49:31+00:00Added an answer on May 15, 2026 at 8:49 am

    You’re making the same incorrect assumption in multiple places: that Stream.Read will return all the data you’ve asked for in one go. You’re ignoring the return value. Don’t do that… admittedly it should be okay for MemoryStreams, assuming there’s enough dataAdditionally, you may want to use the MemoryStream.ToArray method to convert to a byte array.

    Now, you haven’t shown how the client is receiving the string – where are you getting it from?

    You should log the string that the server sends, and the string that the client receives. That’s the absolute first step, so you can see whether the data has actually been preserved.

    I suspect that the client is trying to decompress an empty string – i.e. that Convert.FromBase64 is returning an empty byte array – but until you know the string that’s being received, it’s hard to make any progress.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a game in PHP and jQuery and at some
I am currently working with some users that do not have the .NET Framework
I'm currently working on learning some game development with Java. I'm working on the
I'm currently working on a XNA game prototype. I'm trying to achieve a isometric
I'm currently working on a javascript canvas 2D game engine and I'm having some
I'm currently working on a game, where I need to create a transposition table
I'm currently working on some coding for a hotel booking widget and am using
I am currently working on some Ajax heavy code and I am wondering how
I'm currently working through some exercises in a c++ book, which uses text based
I am currently working on some simple custom allocators in c++ which generally works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.