Sockets: data black holes.

Sometimes, the opacity of the BSD socket interface is a real pain in the backside.

For example, what happens if you do the following:

/* Set a 4k outgoing buffer size */
int bufSize = 8*1024 ;
setsockopt(socket, SOL_SOCKET, SO_SNDBUF,  &bufSize, sizeof(int)) ;
/* Send 8k of data to a host we know will only receive 2k for a while */
retval = send(socket, buffer, bufSize) ;
/* Data is now in the send buffer, but the remote host
only has buffers for 2k, so 6k of our buffer is "hot" */
int shortSize = 2*1024 ;
int retval = setsockopt(socket, SOL_SOCKET, SO_SNDBUF, &shortSize, sizeof(int)) ;
/* Did we just truncate the outgoing send buffer? Or will this generate an error */


You found some old code of Ronald kicking around?
Admittedly FWIR there’s a few free() calls missing :)


If I had to pick from your two options, I’d say “generate an error”, but it might also block until the send buffer can be truncated without loss of data.

As far as I can tell, under Linux, it doesn’t generate an error and it certainly doesn’t block…

What it appears to do is truncate the stream…

For a TCP socket, it would also be technically valid to truncate the buffer as long as all following send()s on the socket fail :)

Leave a Reply

Name and email address are required. Your email address will not be published.

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <pre> <q cite=""> <s> <strike> <strong> 

%d bloggers like this: