title: Sending binary data through beast web-socket (in C++)
body: <p>I want to send <strong>binary audio data</strong> to IBM watson STT service trhough websocket connection. I have successfully made the connection and now trying to send the data in following format:</p>
  
  <pre><code>{
    "action":"start",
    "content-type": "audio/l16;rate=44100"
  }
  &lt;binary audio data&gt;
  {
    "action":"stop"
  }
  </code></pre>
  
  <p><strong>for this:</strong> I am reading a raw audio file (extn <em>.pcm</em>) as below</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;fstream&gt;
  
  ifstream fin;
  string binarydata, bdataline;
  
  fin.open("/home/rohan/TestFile.pcm", ios::binary | ios::in);
  
  while (fin) {
      // Read a Line from File
      getline(fin, bdataline);
      binarydata = binarydata + bdataline;
  }
  </code></pre>
  
  <p><strong>Question 1:</strong> I am not sure if I am reading the binary data correctly. Should the datatype of the <code>binarydata</code> be <code>string</code>?</p>
  
  <p>Next to send the data on boost websocket (after handshake) I have followed this routine</p>
  
  <pre><code>void on_handshake(beast::error_code ec)
      {
           if(ec)
               return fail(ec, "handshake");
  
           // Send the Start message
           ws_.async_write(net::buffer("{\"action\":\"start\",\"content-type\": \"audio/l16;rate=44100\"}"), bind(&amp;session::on_start, shared_from_this(), placeholders::_1));
      }
  
  void on_start(beast::error_code ec)
      {
          if(ec)
              return fail(ec, "write:start");
  
          ws_.async_write(net::buffer(binarydata), bind(&amp;session::on_binarysent, shared_from_this(), placeholders::_1));
      }
  
  void on_binarysent(beast::error_code ec)
      {
          if(ec)
              return fail(ec, "write:Msg");
  
          ws_.async_write(net::buffer("{\"action\":\"stop\"}"), bind(&amp;session::on_write, shared_from_this(), placeholders::_1));
      }
  
  
  void on_write( beast::error_code ec) //,
      {
          if(ec)
              return fail(ec, "write:end");
  
          ws_.async_read(buffer_, bind(&amp;session::on_start, shared_from_this(), placeholders::_1));
      }
  </code></pre>
  
  <p>The program does not show any output and exits with </p>
  
  <blockquote>
    <p>write:start: The WebSocket stream was gracefully closed at both endpoints</p>
  </blockquote>
  
  <p><strong>Question 2:</strong> Whether the data is going correctly as expected? How to check that? (expected : <a href="https://console.bluemix.net/docs/services/speech-to-text/websockets.html#websockets" rel="nofollow noreferrer">See this link</a>)</p>
  
  <p>How is the websocket getting closed without sending close command?</p>
  
  <p><strong>UPDATED:</strong></p>
  
  <pre><code>void on_start(beast::error_code ec)
      {
          if(ec)
              return fail(ec, "write:start");
  
  
  
  ifstream infile("/home/rohan/TestFile.pcm", ios::in | ios::binary);
      streampos FileSize;
  
      if (infile) {
          // Get the size of the file
          infile.seekg(0, ios::end);
          FileSize = infile.tellg();
          infile.seekg(0, ios::beg);
      }
      char binarydata[(size_t)FileSize];
          ws_.binary(true);
  
          // Send binary data
          ws_.async_write(net::buffer(binarydata, sizeof(binarydata)), bind(&amp;session::on_binarysent, shared_from_this(), placeholders::_1));
      }
  </code></pre>