Re: Suggestions for custom application-layer protocol?
From: Grant Edwards (grante_at_visi.com)
Date: Thu, 26 May 2005 18:22:18 -0000
On 2005-05-26, Walter Roberson <firstname.lastname@example.org> wrote:
>:For simplicity of implementation, I'd recommend implementing a message
>:layer over the TCP socket - something as simple as a two byte binary header
>:to indicate the length of the message to follow.
>:This means you can then read a whole message - text based or otherwise,
>:and the process it accordingly. You don't need to mess about with parsing
>:lines and stuff like that.
> You still need to "mess about" if your data might not fit in a single
And even if it does.
> Recall that when you read() from a socket you are not certain
> to get all of the data unless the data length is no greater
> than your system's atomicity limits [PIPE_BUF]. The POSIX
> minimum value for PIPE_BUF is 512 -- so in the general case a
> write() of more than 512 bytes may require multiple read()
The data may have passed through a bridge or router that may
have broken it up even further. It's a bad idea to make any
assumptions about any correspondance between write() and read()
calls on a TCP connection. It's just a byte stream: you
oughtn't depend on any sort of datagram behavior whatsoever.
A 500 byte "message" may have been sent using a single call to
write(), but the receiver had better be prepared to call read()
anywhere from 1 to 500 times.
-- Grant Edwards grante Yow! Two with FLUFFO, at hold th' BEETS...side of visi.com SOYETTES!