Advertisement

Quick question

Started by May 05, 2005 05:04 AM
5 comments, last by hplus0603 19 years, 9 months ago
How can you flush the send buffer used for the sockets? Because if I want to send a message first, and listen for any recieved onces, directly after that, it'll wait for a recieved message before it sends it. EDIT : I use the sockets in C++, both for Windows and Linux.
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
Depending on the IO model, do not modify the buffer until after the data have been processed.

Kuphryn
Advertisement
The TCP send buffer will be automatically flushed after some time, even if Nagle's algorithm and delayed acks are turned on. Thus, it's perfectly safe to send data, and then enter recv(), waiting for a response.

If you think the time-to-flush is too long, then you can turn on TCP_NODELAY on your socket, which means that each call to send() will result in an immediate network packet (unless the window is full).

More information can be had in the Forum FAQ and the resources it references.
enum Bool { True, False, FileNotFound };
Ok, but sometimes I want to fill up a buffer before I send or sometimes I just want it to send after I say "send".

Is there no command in C++ to be able to flush that buffer manually?

Or should I implement one like this:
If the user calls the "flush" method of my Socket class, should I just set that time-to-flush to 0, wait a while (if needed) and reset to 200ms?
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
The only way is the way hplus said, and like he said it may not.
Bummer.
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
Advertisement
You can build your own buffer, and then turn on TCP_NODELAY on the socket. Your buffer flush() function would actually write the data on the socket. This is a fairly easy class to write.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement