title: Setting ASIO no_delay option
body: <p>I'm having troubles setting the no_delay option on an asio socket.  The following code runs well, except for the delay.  My server receives the messages only after the 5000 ms expire.</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  using namespace boost::asio;
  struct Client
  {
     io_service svc;
     ip::tcp::socket sock;
  
     Client() : svc(), sock(svc)
     {
         ip::tcp::resolver resolver(svc);
         ip::tcp::resolver::iterator endpoint = resolver.resolve(boost::asio::ip::tcp::resolver::query("127.0.0.1", "32323"));
         connect(sock, endpoint);
     }
  
     void send(std::string const&amp; message) {
         sock.send(buffer(message));
     }
  };
  
  int main()
  {
     Client client;
  
     client.send("hello world\n");
     client.send("bye world\n");
  
     boost::this_thread::sleep_for(boost::chrono::milliseconds(5000));
  }
  </code></pre>
  
  <p>When trying to add a delay I have a few options:<br>
  1) Add the option before connection:</p>
  
  <pre><code>Client() : svc(), sock(svc)
  {
     ip::tcp::resolver resolver(svc);
     ip::tcp::resolver::iterator endpoint = resolver.resolve(boost::asio::ip::tcp::resolver::query("127.0.0.1", "32323"));
  
     sock.set_option(ip::tcp::no_delay(true));
     connect(sock, endpoint);
  }
  </code></pre>
  
  <p>However this throws <code>set_option: Bad file descriptor</code></p>
  
  <p>2) Add the option after the connection: </p>
  
  <pre><code>Client() : svc(), sock(svc)
  {
     ip::tcp::resolver resolver(svc);
     ip::tcp::resolver::iterator endpoint = resolver.resolve(boost::asio::ip::tcp::resolver::query("127.0.0.1", "32323"));
  
     connect(sock, endpoint);
     sock.set_option(ip::tcp::no_delay(true));
  }
  </code></pre>
  
  <p>However in this case, the option has no effect and I still see the delay.  According to <a href="https://stackoverflow.com/questions/34090155/boostasio-with-no-delay-not-possible">boost::asio with no_delay not possible?</a> , I need to set the option after I've opened the socket but before I've connected the socket.  So I've tried this: </p>
  
  <pre><code>Client() : svc(), sock(svc)
  {
       ip::tcp::endpoint endpoint( ip::address::from_string("127.0.0.1"), 32323);
       sock.open(ip::tcp::v4());
       sock.set_option(ip::tcp::no_delay(true));
       sock.connect(endpoint);
  }
  </code></pre>
  
  <p>However, I still see no effect.  How can I set this option?</p>
  
  <p><strong>Edit:</strong>  It's possible that I am not setting the option correctly on the server-side.  This is the complete server code: </p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;iostream&gt;
  
  int main() {
      boost::asio::io_service        io_service;
      boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 32323));
      boost::asio::ip::tcp::socket   socket(io_service);
  
      acceptor.accept(socket);
      socket.set_option(boost::asio::ip::tcp::no_delay(true));
  
      boost::asio::streambuf sb;
      boost::system::error_code ec;
      while (boost::asio::read(socket, sb, ec)) {
          std::cout &lt;&lt; "received:\n" &lt;&lt; &amp;sb;
      }
  }
  </code></pre>