Writing and reading network data...

A

Adman

Hi all. I've done some websearching, and haven't been able to find my
question answered, so I thought I'd post. I apologize if this has
already been answered.

My question seems to be a variation of a FAQ.

I'm writing a small online game (client/server). The server spits out
a steam of data. The data can be described as a Code, followed by 0 or
many Parameters.

For instance, if the Code is "BulletCreated", then a few Parameters
follow indicating initial position and velocity.

The clients, then, monitor their networkstream. When data is
available, it is read into a buffer, parsed, and the appropriate
actions are taken. For instance, I'll read a byte indicating
"BulletCreated", and then expect to read more bytes for the Parameters.

I believe the problem that I'm having is that, at times, I can read in
the Code, but I'm not necessarily guarenteed to receive it's
accompanying Parameters in the same Read.

I've read various strategies about reading binary data, and there is
some sample code for "reading until the end of the stream", but in this
case, the stream never ends, so it doesn't really apply.

Any ideas of how I can reliably reconstruct my data at the client side?
As I write this, it occurs to me that it's probably more a design
question than a coding question, but I've written all this, I might as
well post it. :)

(ie, perhaps my first piece of data shouldn't be the code, but the
number of bytes that will comprise the next Code/Parameter pair, then I
wait until I have at least that many bytes before continuing...?)

Thanks for any help you can provide!
Adman
 
J

Jon Skeet [C# MVP]

(ie, perhaps my first piece of data shouldn't be the code, but the
number of bytes that will comprise the next Code/Parameter pair, then I
wait until I have at least that many bytes before continuing...?)

That would certainly make things easier, yes. Alternatively, presumably
you should know when you've finished reading a whole section, so just
read until you've read enough. If you use a BufferedStream, it probably
wouldn't hurt your performance hugely if you were to read a byte at a
time, even. (Worth checking though - it's not a particularly attractive
option.)
 
A

Adman

Thanks for the response!

I might have found my error. It appears that there is one instance
where I actually wasn't writing the Code and Parameter out at the same
time. However, the client expected them to both arrive at the same
time, which is why it would choke when trying to read the parameter.

And I'm not reading a byte at a time, actually. I'm doing a Read off
the network stream, and then loading the bytes read into a memory
stream, and doing reads off that. Hope that's not a bad way to do it.

Another question: If I do a networkstream.Write and write out some
data, should I expect to see that same data when I do a corresponding
networksteam.Read? Or could it still be split up across reads? (We're
talking very small amounts of data.... < 30 bytes)

Adman
 
J

Jon Skeet [C# MVP]

Adman said:
Thanks for the response!

I might have found my error. It appears that there is one instance
where I actually wasn't writing the Code and Parameter out at the same
time. However, the client expected them to both arrive at the same
time, which is why it would choke when trying to read the parameter.

And I'm not reading a byte at a time, actually. I'm doing a Read off
the network stream, and then loading the bytes read into a memory
stream, and doing reads off that. Hope that's not a bad way to do it.

That's okay - but I think it's exactly what a BufferedStream is doing
under the covers, really.
Another question: If I do a networkstream.Write and write out some
data, should I expect to see that same data when I do a corresponding
networksteam.Read? Or could it still be split up across reads? (We're
talking very small amounts of data.... < 30 bytes)

I wouldn't like to say, to be honest. I certainly wouldn't *rely* on
getting it all in one go, even though I'd expect it to be okay.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top