Re: Segmented Data Over Sockets

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
|
|


.



Relevant Pages

  • Re: How to determine integer values in a txt file?
    ... the fixed header, skip 2 bytes and read the 5th byte to determine how many ... The problem boils down to one of positioning and then reading - how it can ... the stream then the task is easy - just read the header in! ... really, quite easy to accomplish. ...
    (comp.lang.java.programmer)
  • Re: Segmented Data Over Sockets
    ... You can still get the header in two pieces, ... Microsoft MVP, MCSD ... only difference here is now your adding boundaries to the stream. ... |> each byte of the local structure on the receiving end, ...
    (microsoft.public.win32.programmer.networks)
  • Re: write eof without closing
    ... It appears that receiving an EOF from a stream ... tells it when to stop 'reading', not necessarily that the stream is ...
    (comp.lang.python)
  • Diff b/w EIA-708 and CEA-708?
    ... We are receiving 708 data in VANC area. ... Currently I am reading ... could find header for 708. ...
    (alt.usage.english)
  • Re: direct access with Intel fortran
    ... This approach would work if I placed the header block at the end of the file, but not if it is at the start of the file as writes using 'binary' format truncate the file. ... But even for formats I do control, I prefer having the information at the start of the file as I can then set up all of the data arrays immediately after opening the file and before actually reading the data. ... file access are still grounded in the exigencies of reading tapes, and in that context truncate after write presumably makes perfect sense. ...
    (comp.lang.fortran)