Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to flush a POSIX socket?

Tags:

c

posix

sockets

Is there a standard call for flushing the transmit side of a POSIX socket all the way through to the remote end or does this need to be implemented as part of the user level protocol? I looked around the usual headers but couldn't find anything.

like image 319
Gordon Wrigley Avatar asked May 13 '09 00:05

Gordon Wrigley


People also ask

How do you flush a TCP socket?

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.

What is a Posix socket?

A POSIX Socket or simply a Socket is defined as a communication endpoint. For example, if two parties, A and B, intend to communicate with each other, then it will be required that both of these parties establish a connection between their respective endpoints.

Can reuse a closed TCP sockets?

You can re-use the socket handle. Once you call close , regardless of what happens to the actual socket, the handle can be reused. Your next call to socket or accept may get back the same handle. (But don't assume it does, just store the handle.)

Do sockets have buffers?

A socket has two buffers and some other information associated with it. In the context of sockets programming, a socket is your app's interface to one TCP connection (or UDP flow). Your app doesn't read/write data from/to the network interface card (NIC) directly, it goes through the kernel's network stack.


2 Answers

What about setting TCP_NODELAY and than reseting it back? Probably it could be done just before sending important data, or when we are done with sending a message.

send(sock, "notimportant", ...); send(sock, "notimportant", ...); send(sock, "notimportant", ...); int flag = 1;  setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)); send(sock, "important data or end of the current message", ...); flag = 0;  setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)); 

As linux man pages says

TCP_NODELAY ... setting this option forces an explicit flush of pending output ...

So probably it would be better to set it after the message, but am not sure how it works on other systems

like image 65
Fantastory Avatar answered Oct 08 '22 19:10

Fantastory


For Unix-domain sockets, you can use fflush(), but I'm thinking you probably mean network sockets. There isn't really a concept of flushing those. The closest things are:

  1. At the end of your session, calling shutdown(sock, SHUT_WR) to close out writes on the socket.

  2. On TCP sockets, disabling the Nagle algorithm with sockopt TCP_NODELAY, which is generally a terrible idea that will not reliably do what you want, even if it seems to take care of it on initial investigation.

It's very likely that handling whatever issue is calling for a 'flush' at the user protocol level is going to be the right thing.

like image 24
chaos Avatar answered Oct 08 '22 19:10

chaos