469,632 Members | 1,698 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

Writing and reading network data...

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

Nov 17 '05 #1
3 1535
Adman <as*****@yahoo.com> wrote:

<snip>
(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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
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

Nov 17 '05 #3
Adman <as*****@yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Jeff Pritchard | last post: by
16 posts views Thread by Ali | last post: by
4 posts views Thread by William Sullivan | last post: by
4 posts views Thread by John Pote | last post: by
16 posts views Thread by Claudio Grondi | last post: by
6 posts views Thread by arne.muller | last post: by
3 posts views Thread by Barry Flynn | last post: by
10 posts views Thread by puzzlecracker | last post: by
11 posts views Thread by Krzysztof Retel | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.