Re: Segmented Data Over Sockets
- From: "William Stacey [C# MVP]" <william.stacey@xxxxxxxxx>
- Date: Thu, 14 Dec 2006 00:36:16 -0500
That, IMO, is the way to go. Forget about packets and rounters, etc. Think
only at the stream level. From that perspective, it is exactly like reading
from a file. Keep reading bytes until you receive 0. Same thing here. The
only difference here is now your adding boundaries to the stream. So
prepend an int length in the header which says how many data bytes to follow
header. A real simple header will only be an int (i.e. the data length to
follow). But your header could also include things like DataType,
request/reply flags, etc. In any case, your header will be a known fixed
size, so you always can read 10 bytes (for example) and know you got the
header. Parse header, and keep reading until you get N data bytes and then
you got the whole message. Do it again and again until you read 0 or some
error.
--
William Stacey [C# MVP]
"DJX" <djx@xxxxxxxxxxxxxxxx> wrote in message
news:u6GaTzyHHHA.2632@xxxxxxxxxxxxxxxxxxxxxxx
| Just as others explained, if you send() N bytes from the client, it does
not
| mean that a single corresponding recv() on the server will receive N
bytes.
| It could receive less, or it could receive more if multiple send()'s have
| been issued.
|
| The only thing that is "guaranteed" is that the bytes will come to the
| server in the same order in which they were sent.
|
| I would also suggest that you send the struct length as part of a "header"
| and use that to read the struct data instead of assuming a struct length
of
| 260. That way you could support multiple versions of the client software
| should you need to change the size of the struct in the future.
|
|
| "Saul775" <Saul775@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
| news:E731E35D-27A2-486B-A201-28B570F5D19E@xxxxxxxxxxxxxxxx
| > Hello, All:
| >
| > I've -- hopefully -- one last question to ask about sockets.
| >
| > I'm sending a structure over a socket from one computer to another;
| > however,
| > the router is breaking the stream into segments. I'd like to recombine
| > the
| > data on the receiving end. What is the accepted practice, or method, in
| > recombining data, so the structure is complete?
| >
| > I've an idea, but I'm not sure if it's the accepted method. I will
| > receive
| > the data. If it's not the size -- in bytes -- of the structure, I will
| > parse
| > each byte of the local structure on the receiving end, copying bytes.
| > Then I
| > will continue to recv() until the amount of bytes received is equal to
the
| > size of the structure. Again, this is just an idea, but I'm not sure if
| > there's an easier method or, better yet, a function that will
concatenate
| > the
| > two byte streams for me and place it into my structure.
| >
| > Thank you, all.
| >
| >
| > Saul775
|
|
.
- Follow-Ups:
- Re: Segmented Data Over Sockets
- From: Alexander Nickolov
- Re: Segmented Data Over Sockets
- References:
- Re: Segmented Data Over Sockets
- From: DJX
- Re: Segmented Data Over Sockets
- Prev by Date: Re: Segmented Data Over Sockets
- Next by Date: Network for beta test - build, rent, emulate?
- Previous by thread: Re: Segmented Data Over Sockets
- Next by thread: Re: Segmented Data Over Sockets
- Index(es):
Relevant Pages
|