boost::asio with no_delay not possible?

旧巷老猫 提交于 2019-12-20 03:43:20

问题


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.


回答1:


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);



回答2:


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));


来源:https://stackoverflow.com/questions/34090155/boostasio-with-no-delay-not-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!