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 7868585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:03:00+00:00 2026-06-03T01:03:00+00:00

I’m running into a strange issue trying to use ByteArrays to send an object

  • 0

I’m running into a strange issue trying to use ByteArrays to send an object and unsigned int over a Socket. I’m trying to handle a case where I may receive data in multiple packets.

Here is my socket data handler:

protected function handleSocketData(e:ProgressEvent):void{
        var s:Socket = e.target as Socket;

        trace("pre if " + len + " " + s.bytesAvailable);
        if (len == 0 && s.bytesAvailable >= 4) {
            len = s.readUnsignedInt();

        }

        trace(len + " " + s.bytesAvailable);
        if (len > 0 && s.bytesAvailable >= len){
            var ba:ByteArray = new ByteArray();
            //s.readBytes(ba, 0, len);
            s.readBytes(ba, 0, s.bytesAvailable); //should be len

            //trace("handle socket before inflate " + ba.length);
            ba.inflate();
            //trace("handle socket after inflate " + ba.length);
            var o:Object = ba.readObject();
            Overlord.updatePlayers(o);
            trace(o.player);
            len = 0;


        }
}

The problem lies with s.readBytes(ba, 0, len);

I’m using len (defined at the class scope) to handle a case where I may get multiple packets. Now this seems to work the first time I get data over the socket.

pre if 0 44 - trace I'm getting before the first if statement
38 40 - trace I'm getting after
frank - Everything comes in fine, working!

Now if I send data over the socket again:

**first send:**
pre if 0 44
38 40
frank - working, yay!
**things start to fail every send after:**
pre if 0 46
218759168 42
pre if 218759168 84
218759168 84
pre if 218759168 127
218759168 127

Now it seems like readBytes is leaving data in the buffer.

The strange part is when I swap s.readBytes(ba, 0, len); for s.readBytes(ba, 0, s.bytesAvailable); everything seems to work:

With s.readBytes(ba, 0, s.bytesAvailable);

**first send**
pre if 0 44
38 40
frank
**everything seems to work after, no old data in the buffer?**
pre if 0 44
38 40
frank
pre if 0 43
37 39
frank

The issue with using bytesAvailable is that now I can’t handle multiple packets.

pre if 2214 2218
2214 2218
RangeError: Error #2006: The supplied index is out of bounds.
at flash.utils::ByteArray/readObject()

I’m not quite sure what I’m missing here, it seems like the buffer is only getting cleared when I use socket.bytesAvailable. Anyone have any input?

edit

I’m using this method for sending data over the socket. By writing the length of the ByteArray and reading it back in the data handler I’m trying to handle getting the data object across multiple packets.

        public function sendData(socketData:*):void{
        if(_connected){
            //trace("sending");
            var ba:ByteArray = new ByteArray();
            ba.writeObject(socketData);
            ba.position = 0;
            //trace("byteArray len before send and defalte: " + ba.length)
            ba.deflate();
            //trace("byteArray len before send and after defalte: " + ba.length)
            socket.writeUnsignedInt(ba.length);
            socket.writeBytes(ba, 0, ba.length);
            socket.flush();
        } else {
            throw new Error("Socket not open");
        }
    }
  • 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-06-03T01:03:01+00:00Added an answer on June 3, 2026 at 1:03 am

    You should really pull the data out of the socket into a byte array and then do your manipulations on the bytearray and not the socket. When you call any of the read***() methods on the socket or a bytearray, you move the .position property by the value of your read method. This is also the reason why you’re getting the supplied index error on readObject. Before calling readObject, set the .position property of BA to 0 first.

     ba.position = 0;
     var o:Object = ba.readObject();
    

    My comment on your question and the details in this answer I believe are at the very least a fundamental issue with your code. You should also post the code of the sending socket so that we can figure this out easier.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:

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.