NetBSD-Users archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: minimum write size of SOCK_STREAM?
Date: Fri, 31 Oct 2025 11:48:50 -0400
From: Greg Troxel <gdt%lexort.com@localhost>
Message-ID: <rmizf97138d.fsf%s1.lexort.com@localhost>
| There is no reason to expect that read on the peer will return once with
| the full message, even though doing so would be typical.
Fully correct - applications that expect a certain amount of data need
to be able to repeat the read (with subsequent reads appending to what
was read before) until the expected amount data (or EOF, or error) is
returned.
What's more, with SOCK_STREAM there's no guarantee that a read will contain
only data from a single previous write (if the sender writes more than once
of course). The read is allowed to return (say) the final part of the
first write (assuming the initial part had been returned earlier) and
the initial part of the following one, or perhaps even all of it, along
with some of the next (etc).
If boundaries between "packets" are needed, the application needs to
supply a method of providing them (a common way is to write the length
immediately before the data) and then interpret the data using those
boundaries when reading (eg: read the length first, then arrange to read
exactly that number of bytes, in as many sys call as is required).
Think of writing into a file, data can be written one byte at a time, or
the whole file in one write, or anything in between - the resulting file
is identical in all cases. Something reading the file cannot determine
which method was used to perform the writes. SOCK_STREAM is just like that.
The minimum meaningful write size is 1 byte (8 bits) - writes of less than
1 byte (but more than 0) cannot be expressed in the API (the size field is
in bytes, not bits) and writes of length 0 are generally meaningless (in
some protocols (or to some devices) they can have a meaning).
kre
Home |
Main Index |
Thread Index |
Old Index