Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to flush a socket?

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?

like image 870
Bjorn Avatar asked May 27 '09 06:05

Bjorn


People also ask

How do I flush a socket in Linux?

TCP_NODELAY ... setting this option forces an explicit flush of pending output ... This should work with linux at least; when you set TCP_NODELAY it flushes the current buffer, which should, according to the source comments, even flush when TCP_CORK is set. I confirm that it works.

How do you clear a socket in C++?

There is no "flush" functionality for sockets. If you need to send two messages in rapid succession then just send them. If it's a TCP socket then they will arrive in the correct order (the order you send them in).

What happens when you close a socket?

close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed.


2 Answers

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.

like image 168
BJ Homer Avatar answered Oct 11 '22 23:10

BJ Homer


You cant really flush a socket.

(From How can I force a socket to send the data in its buffer?)

You can't force it. Period. TCP makes up its own mind as to when it can send data. Now, normally when you call write() on a TCP socket,
TCP will indeed send a segment, but there's no guarantee and no way to force this. There are lots of reasons why TCP will not send a
segment: a closed window and the Nagle algorithm are two things to
come immediately to mind.

Read the full post, it is quite in-depth and clarified a lot of the things for me.

like image 44
Alexei Tenitski Avatar answered Oct 11 '22 22:10

Alexei Tenitski