tags:

views:

1876

answers:

3

I don't really know much about sockets except how to read and write to them as if they were files. I know a little about using socket selectors. I don't get why you have to flush a socket, what's actually happening there? The bits just hang out somewhere in memory until they get pushed off? I read some things online about sockets, but it's all very abstract and high level.

What's actually happening here?

+8  A: 

There's a certain amount of overhead involved in writing to a network socket and sending data. If data were sent every time byte entered the socket, you'd end up with 40+ bytes of TCP header for every byte of actual data. (Assuming you're using a TCP socket, of course. Other sockets will have different values). In order to avoid such inefficiency, the socket maintains a local buffer, which is usually somewhat over 1000 bytes. When that buffer is filled, a header is wrapped around the data and the packet is sent off to its destination.

In many cases, you don't need each packet to be sent immediately; if you're transferring a file, early data may not be of any use without the final data of the file, so this works well. If you need to force data to be sent immediately, however, flushing the buffer will send any data which has not yet been sent.

Note that when you close a socket, it automatically flushes any remaning data, so there's no need to flush before you close.

BJ Homer
how does NAGL tie into this, do you know?
rogerdpack
+1  A: 

I haven't used flush for socket programming. I do remember somewhere that there are modes that you can set, where stream oriented sends try to reduce the number of small packets sent. However, you'd use a sendall() to make sure that everything in the buffer is sent.

The shutdown() function is very useful as you are spooling down. Of course you must use a close() call if you don't want stuff hanging around.

Have a look at Beej's guide for more information.

CyberED
sendall() is the same as flush()
Georg
+1  A: 

Nagle's algorithm is often in use on sockets. In a nutshell, it waits until there's a non-trivial amount of data to send. The problem is to achieve a balance between transmission latency and the overhead cost of sending a packet of data.

The larger the data payload, the smaller the wasted bandwidth because the header is of (mostly) fixed size. Furthermore intermediate systems generally have performance limits based more significantly on the packet-rate, not the overall data-rate.

caskey
This answer doesn't really address how flushing a socket ties in, though.
Quinn Taylor