Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > calling read() in blocks
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 23rd April 14:10
jens.toerring
External User
 
Posts: 1
Default calling read() in blocks



Since what you read from the pipe is an unstructured stream of bytes
there's probably nothing what you wouldn't call a "makeshift" solution.
An alternative to your scheme would obvioulsy be to prepend each
message with a fixed number of bytes telling the other side how
long the message is going to be. If the number is e.g. 2 bytes you
would first read these 2 bytes, so you know how many bytes to read
in the second call of read().

All other methods would require some additional IPC, e.g. having the
sender wait for a signal by the reader before sending another message,
so the reader always knows there's no more than a single message in
the pipe.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Jens.Toerring@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
  Reply With Quote


  sponsored links


2 23rd April 14:10
floyd davidson
External User
 
Posts: 1
Default calling read() in blocks



There is a simple solution, if and only if what you are sending
is always just ascii text.

Append a newline to the end of each block sent, thus making it
line oriented.

At the receiving end, use fdopen() on your socket to get a FILE*,
and then use line oriented functions, like fgets() to retrieve
data from the socket. --
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
  Reply With Quote
3 23rd April 14:10
kevin easton
External User
 
Posts: 1
Default calling read() in blocks


That's not a makeshift solution - it's one of the right solutions.
pipes don't implement a datagram-like message passing protocol, they
implement a stream. So, you need to impose your own framing protocol on
top.

- Kevin.
  Reply With Quote
4 23rd April 14:11
External User
 
Posts: 1
Default calling read() in blocks


I usually use a simple protocol in this type of message passing. Make the
first 1, 2 or 4 bytes be the length of the rest of the message. First you
read the length (1, 2 or 4 bytes) and then you read the message.
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666