iostream和No_delay选项 [英] iostream and No_delay option

查看:77
本文介绍了iostream和No_delay选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用相同问题的答案禁用Nagle算法: ASIO ip: :tcp :: iostream和TCP_NODELAY :

I am trying to disable the Nagle Algorithm using the answer for the same question: ASIO ip::tcp::iostream and TCP_NODELAY:

    boost::asio::ip::tcp::iostream socketStream;
    const boost::asio::ip::tcp::no_delay option( true );
    socketStream.rdbuf()->set_option( option );
    boost::asio::io_service io_service;
    tcp::endpoint endpoint (tcp::v4 (), 6666);
    tcp::acceptor acceptor (io_service, endpoint);

    std::cout << "Waiting for connection.." << std::endl;
    acceptor.accept (*socketStream.rdbuf ());
    std::cout << "Connected!" << std::endl;

,并且在运行代码时出现此错误:

and when running the code this error appears:

set_option: Bad file descriptor

我该如何解决这个问题?

How can I solve this problem?

推荐答案

在设置选项的地方,流仍然无效(未打开).

Where you set the option, the stream is still invalid (not open).

在设置选项之前,请等待直到插座打开:

Wait until the socket is open, before setting the option:

在Coliru上直播

#include <boost/asio.hpp>
#include <iostream>

static boost::asio::ip::tcp::no_delay const no_delay_option (true);

int main() {
    using boost::asio::ip::tcp;

    tcp::iostream socketStream;


    boost::asio::io_service io_service;

    tcp::endpoint endpoint (tcp::v4(), 6666);
    tcp::acceptor acceptor (io_service, endpoint);

    std::cout << "Waiting for connection.." << std::endl;
    acceptor.accept (*socketStream.rdbuf ());
    socketStream.rdbuf()->set_option(no_delay_option);

    std::cout << "Connected!" << std::endl;
    std::cout << socketStream.rdbuf() << "\n";
}

(我们在那里使用netcat将main.cpp发送到端口6666)

(We send main.cpp to port 6666 using netcat there)

这篇关于iostream和No_delay选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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