我正在尝试使用相同问题的答案来禁用Nagle算法: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;
并且在运行代码时出现此错误:
set_option: Bad file descriptor
我该如何解决这个问题?
答案 0 :(得分:1)
在您设置选项的情况下,流仍然无效(未打开)。
等待套接字打开,然后再设置选项:
<强> Live On 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)