Home > Coding > Sockets: data black holes.

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 */
Categories: Coding Tags: ,
  1. mike wallis
    September 6, 2011 at 3:37 pm

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

  2. September 6, 2011 at 4:21 pm


    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.

  3. Sres
    September 6, 2011 at 4:35 pm

    higgs boson?

  4. September 6, 2011 at 6:56 pm

    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…

  5. Tuure Laurinolli
    September 7, 2011 at 12:46 am

    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 :)

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: