1

having a bit of an issue here.

I am working on a multiplayer game with flash, and using python for the server side of things. I have the socket connection working... sorta, and quite a bit of the python work done, but I am running into a weird problem.

Let's say, upon logging in, I send some data to the client containing some of their info. After which, I send some data to assign them to a room.

This data doesn't seem to be read in AS3 as two different things, instead, after readUTFBytes, it is all in the same string.

var str:String = event.currentTarget.readUTFBytes(event.currentTarget.bytesAvailable);

In python, I have defined methods for sending data, which just sends data via transport.write (Twisted), and I am receieving via a progress socket data event in action script. Any idea what could be wrong here? Here's a bit of code...

if ( ! event.currentTarget.bytesAvailable > 0) {
            return;
        }
        var str:String = event.currentTarget.readUTFBytes(event.currentTarget.bytesAvailable);
        var Char1:String = str.charAt(0);
        var Char2:String = str.charAt(1);
        str = str.replace(Char1, "");
        str = str.replace(Char2, "");

        // Various messages
        if (Char1 == "\x03") {
            if (Char2 == "\x03") {
                trace("Got ping thread");
            }
            else {
                trace("x03 but no secondary prefix handled");
            }
            return;             
        }

Quite sloppy I know, but I'm trying to just determine an issue.

All data comes with two prefixes, something like \x02 and \x09 for me to determine what to do, then most data in the string is split on \x01 to get values.

The problem essentially is, where I should get an /x08 /x08 data, I get /x08 /x08 data /x05 /x03 data, when it should be two separate things.

WeaponsTheyFear
  • 71
  • 2
  • 2
  • 9

1 Answers1

2

TCP connections are reliable, ordered, stream oriented transports. A stream is a sequence of bytes with no inherent message boundaries. If you want to split up your bytes into separate messages, the bytes themselves must tell you how to do this splitting (or you need some external rule that always applies, like "a message is 5 bytes long").

This applies to all TCP connections, regardless of what language you use them from, or what weird library-specific API gets dropped on top of them (like readUTFBytes).

There are many options for protocols which can help you frame your messages. For example, you could use a length prefix. Then your messages would look like:

\x07 \x08 \x08 h e l l o \x05 \x05 \x03 m a n

\x07 gives the length of the first message, 7 bytes: \x08 \x 08 h e l l o. The next byte after that message, \x05, gives the length of the second message: \x05 \x03 m a n.

You can use multibyte length prefixes if your messages need to be longer, or netstrings which use a decimal representation and a : delimiter to support arbitrary sized prefixes. There are also more sophisticated protocols which offer more features than just separating your bytes into messages. For example, there's AMP which gives you a form of RPC with arguments and responses.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
  • Thank you, this clears up quite a bit. Someone also suggested using terminators, but I think I would like to learn the more complicated methods before hand. Thanks again! – WeaponsTheyFear Dec 06 '11 at 23:18
  • +1 for prefixing message length and performing your own splitting on the as3 side. the as3 sockets aren't great – divillysausages Jan 15 '12 at 16:10