Home > Coding > Socket buffers

Socket buffers

If you want to experiment with code for my earlier question about setsockopt and send buffer truncation, I’ve written a small test program (Linux based, might work on Mac too tho, shouldn’t be too hard to port to Winsock)

http://www.kfs.org/oliver/socket.cpp

I haven’t had to look at sockets this way for a very long time, but now that I am, I’m remembering how bloody annoying the supposedly ultra-clean and simple BSD socket API is…

Windows socket behavior seems to be somewhat different from BSD. For instance,

#if defined(_MSC_VER)
typedef HANDLE socket_t ;
#define NON_BLOCK() ioctlsocket(socket, FIONBIO, &val)
#else
typedef int socket_t ;
#define NON_BLOCK() fcntl(socket, F_SETFL, fcntl(socket, F_GETFL, 0) | O_NONBLOCK) ;
#endif

void setNonBlocking(socket_t socket)
{
int val = 1 ;
NON_BLOCK() ;
setsockopt(socket, SOL_TCP, TCP_NODELAY, (const char*)&val, sizeof(val)) ;
}

static const size_t dataSize = 32 * 1024 ; // 32k.
char buffer[dataSize] ;  // 8k buffer.
memset(buffer, 0, sizeof(buffer)) ; // Clear it out.
// Specifically: make the send buffer way too small.
unsigned int bufSize = dataSize / 8 ;
setsockopt(socket, SOL_SOCKET, SO_SNDBUF, (const char*)&bufSize, sizeof(bufSize)) ;
// Enable non-blocking behavior, so write only commits to
// the amount of data that can be copied into the send buffer.
setNonBlocking(socket) ;

// If we try to write all of this data, there should be insufficient
// buffer space.
int ret = write(socket, buffer, dataSize) ;

// Under linux, "ret" will be somewhere around "bufSize".
// Under Windows, it somehow manages to find buffer space
// and "ret" is "dataSize". Huh?

Under Linux, there’s also no way to tell how much data is in the buffer (I haven’t checked to see if WinSock2 provides a way).

It would be handy to be able to tell, since it’d let you detect a stall, but it would also allow you to be smart about buffer management.

For instance: you might have a single socket which is generally only doing small amounts of transfer. But then you need to schlep across a big burst of data.

Categories: Coding Tags: ,
  1. Coubo
    September 6, 2011 at 9:54 pm | #1

    Standard socket functions make up one of the most annoying API around…
    It’s infuriating that there is so little control on the way the system manages those socket and the lack of proper assynchronous mode is like a no go for any major server application. I really don’t know what the people who built those functions were thinking. Probably they did not imagine that in the future people would play MMO where a server would have to handle thousands of simultanous datastreams.

    I believe the MAJOR reason why most MMOs are laggy and have connection issues is due to this crappy API

    Winsocks is much, much, better but has no portability.

  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

Follow

Get every new post delivered to your Inbox.

Join 183 other followers

%d bloggers like this: