5

What I know...

I need to call set_option(tcp::no_delay(true)) before connect() according to https://stackoverflow.com/a/25871250 or it will have no effect.

Furthermore, set_option() works only if the socket was opened beforehand according to https://stackoverflow.com/a/12845502.

However, the documentation for async_connect() states that the passed socket will be closed if it is open before handling the connection setup (see async_connect()).

Which means that the approach I chose does not set NO_DELAY correctly (I have tested this on Windows 7 x64 so I can say for sure).

if ( socket.is_open() ) {
    socket.close();
}
socket.open(tcp::v4());
socket.set_option(tcp::no_delay(true));
socket.async_connect(endpoint, bind(&MySession::connectComplete, this, asio::placeholders::error));


Question: How can I set NO_DELAY with Boost ASIO correctly to open a client connection?


P.S.: I am using Boost 1.53. Switching to another Boost version is not easiliy possible for me.

P.P.S.: Not setting NO_DELAY in my program but for the network interface in the registry solves this issue but this will affect all applications which is not my intention. See description.

Community
  • 1
  • 1
user2525536
  • 366
  • 1
  • 4
  • 14

2 Answers2

4

The async_connect() free function will close the socket:

If the socket is already open, it will be closed.

However, the socket.async_connect() member function will not close the socket:

The socket is automatically opened if it is not already open. If the connect fails, and the socket was automatically opened, the socket is not returned to the closed state.

The following code will set the no_delay option on an open socket, and then initiate an asynchronous connect operation for the open socket:

socket.open(tcp::v4());
socket.set_option(tcp::no_delay(true));
socket.async_connect(endpoint, handler);
Tanner Sansbury
  • 51,153
  • 9
  • 112
  • 169
  • Seems that I took the wrong documentation entry. However, it does not explain why I still get the delayed TCP ACK. Does that work for you? – user2525536 Dec 04 '15 at 17:19
  • 1
    It works for me ([demo](http://coliru.stacked-crooked.com/a/cf64e43467bb1a36)). Perhaps you can use the small demo program to verify if the option is properly being set. – Tanner Sansbury Dec 04 '15 at 17:34
1

Just set it right after connection. Nagle algorithm works after you send any data before kernel received ASK packet. So it does not matter for connect operation. Just set it right after connect, before any send.

socket.async_connect(ep, yield);
socket.set_option(tcp::no_delay(true));
Galimov Albert
  • 7,269
  • 1
  • 24
  • 50