0

If I have a definition which is only repeated strings, I can find the length of the packed buffers via the get_packed_size call. However, if I am on the receiving side of the exchange, how do I know how many bytes to read to form a complete message? (Since there are a variable number of entries, it isn't known apriori.)

Sender:

length = <name>_get_packed_size(&message)
buffer = malloc(length)
<name>_pack(&message, buffer)
write(fd, buffer, length)

Receiver:

read(fd, buffer, ???)       // what is '???' if 'fd' is a stream socket?

If I am in datagram mode, I can issue the read for something like 64K bytes and just get the entire message. However, if I am in stream mode, how do I do this without short changing the message or reading part of the next message?

No One in Particular
  • 2,846
  • 4
  • 27
  • 32

1 Answers1

1

See this answer for a typical solution to this common problem: https://stackoverflow.com/a/5586945/618259

Community
  • 1
  • 1
alavrik
  • 2,151
  • 12
  • 16