XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

how professionals delimit networkstream data?

Last post 9/28/2009 4:28 PM by jwatte. 2 replies.
  • 9/28/2009 3:54 PM

    how professionals delimit networkstream data?

    I wanted to know an effiecient way to delimit the data I am sending in network streams while maintaining the smallest possible size of the message.  I am sending back to back messages and need a way to separate them.

    Also I have a feeling someone is going to say, use the length/size of message to split them, which I have no problem doing it if is the best and most effiencient way.  And if size is the best way, what is the best size to send excluding headers.  please give answer in bytes.
  • 9/28/2009 4:28 PM In reply to

    Re: how professionals delimit networkstream data?

    The most efficient delimiter is no delimiter, use fixed width fields in a fixed order, then parse them back apart the same way.

    Is this for the XBox?  If yes, then it will be using UDP which is a packet based protocol, no need for a header with the length of the packet.

    If this is for Windows, then it could be TCP or UDP.  TCP is a streaming protocol, so you would need a header with the length stored so that you will know where the end of the packet is.  The size in bytes to use for the length depends on how big your packets will be., if your packets are always going to be smaller than 256 bytes in length, then use a single byte, if they are going to be from zero to 65536 bytes in length, use two bytes, if it is going to be longer use three or four bytes.

    It really depends on what you are doing, and how you are doing it, there is no right or wrong answer here, do what works for your game.
  • 9/28/2009 4:28 PM In reply to

    Re: how professionals delimit networkstream data?

    There are two ways to delimit:

    1) size/data
    2) implicit length

    The implicit length can be used if you know how long each message will be, based on the first byte or two of the message. For example, if you identify message types with a byte, then you can read a single byte, and then dispatch to a handler based on that value. The handler would know how to read the data for the message. Each data type within the message (string, blob, etc) would have to be perfectly delimited or length-prefixed, though, or you wouldn't know where a string ended, for example.

    In general, it's easier to deal with data where you already know you've read the right amount, so even if it's less efficient, size/data is often used. To encode integers, especially for size, I often use a "continuation bit" method. I store 7 bits in each byte, and if I haven't stored enough bits yet, I set the high bit, and store another 7 bits in the next byte. Repeat as needed :-) (For signed numbers, you also want to store the sign bit in the second-highest bit of the first byte for negative numbers, but sizes are never signed.)


    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
Page 1 of 1 (3 items) Previous Next