没有no_delay的boost :: asio不可能吗? [英] boost::asio with no_delay not possible?

查看:265
本文介绍了没有no_delay的boost :: asio不可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所知道的...

我需要根据 https://stackoverflow.com/a/25871250 connect()之前致电set_option(tcp::no_delay(true))它不会有效果.

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.

此外,只有根据 https://stackoverflow.com/a/12845502.

但是,async_connect()的文档指出,如果在处理连接设置之前打开了所传递的套接字,它将被关闭(请参阅

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()).

这意味着我选择的方法无法正确设置NO_DELAY(我已经在Windows 7和x64上进行了测试,因此可以肯定地说).

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


问题:如何通过Boost ASIO正确设置NO_DELAY以打开客户端连接?


附注:我使用的是Boost 1.53.对我来说,切换到另一个Boost版本是不容易的.


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

P.P.S .:在我的程序中未设置NO_DELAY而是在注册表中为网络接口解决了此问题,但这将影响所有应用程序,这不是我的意图.参见说明.

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.

推荐答案

The async_connect() free function will close the socket:

如果套接字已经打开,它将被关闭.

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

但是, socket.async_connect() 成员函数不会关闭套接字:

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.

以下代码将设置 no_delay 选项在打开的套接字上,然后为打开的套接字启动异步连接操作:

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

这篇关于没有no_delay的boost :: asio不可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆