C++ 通过beast web套接字发送二进制数据(在C+;+;)

C++ 通过beast web套接字发送二进制数据(在C+;+;),c++,file-io,websocket,binaryfiles,boost-beast,C++,File Io,Websocket,Binaryfiles,Boost Beast,我想通过websocket连接向IBM watson STT服务发送二进制音频数据。我已成功建立连接,现在尝试以以下格式发送数据: { "action":"start", "content-type": "audio/l16;rate=44100" } <binary audio data> { "action":"stop" } 程序不显示任何输出,并退出 write:start:WebSocket流在两个端点都正常关闭 问题2:数据是否按预期正确运行?如何检查?(预

我想通过websocket连接向IBM watson STT服务发送二进制音频数据。我已成功建立连接,现在尝试以以下格式发送数据:

{
  "action":"start",
  "content-type": "audio/l16;rate=44100"
}
<binary audio data>
{
  "action":"stop"
}
程序不显示任何输出,并退出

write:start:WebSocket流在两个端点都正常关闭

问题2:数据是否按预期正确运行?如何检查?(预期:)

websocket如何在不发送close命令的情况下关闭

更新:

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(&session::on_binarysent, shared_from_this(), placeholders::_1));
    }

答案1:

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(&session::on_binarysent, shared_from_this(), placeholders::_1));
    }
关于问题的websocket部分,您应该通过调用
websocket::stream::binary(true)
来确保发送的是二进制消息。见:

答案2:

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(&session::on_binarysent, shared_from_this(), placeholders::_1));
    }
发件人:

在读取操作期间收到关闭帧时,实现将自动响应关闭帧,然后在返回之前关闭基础连接。在这种情况下,读取操作将以错误代码::closed完成。这向调用方表明连接已完全关闭

答案3(更新)

你写道:

vector<char> binarydata(istreambuf_iterator<char {infile}, {});

vector binarydata(istreambuf_迭代器您不能使用
string
来存储二进制数据,请使用
char data[]
int length
std::vector
或其他任何内容

只要二进制文件中没有行,就不应使用
getline()
读取二进制数据

您可以使用以下内容:

std::ifstream f("/home/rohan/TestFile.pcm", std::ios::binary);
std::vector<char> v(std::istreambuf_iterator<char>{f}, {});
std::ifstream f(“/home/rohan/TestFile.pcm”,std::ios::binary);
std::vector v(std::istreambuf_迭代器{f},{});

我已经按照你的建议更新了这个问题。通过这些更新,程序并没有“正常”关闭连接。相反,它是以值
-1退出的。调用函数写得正确吗?
ws.binary(true);
我已经更新了帖子。请看一看。
(net::buffer(binarydata,sizeof(binarydata))
现在程序以值
0退出了。我已经更新了帖子。正如您所看到的,我已经做了更改。我省略了以前的文件读取方式(不正确的方式)。感谢您的更正。有没有办法查看
binarydata
是否包含二进制数据?您是什么意思“保存二进制数据”?任何数据都可以是二进制的,但不能是任何-文本。因此,无论
binarydata
中有什么,它都是二进制数据。有时您可以假设数据是文本,但它们并不严格,并且取决于可能的文本编码。感谢您的澄清:)
std::string
完全可以保存二进制数据,包括空值。我使用了char数组来获取二进制数据。你可以在更新的帖子中查看我是如何读取二进制文件的。这对我来说很有用。尊重他人时间的一个好方法是按照Beast文档中关于熟悉Asio。这些问题与Beast无关。嗨@VinnieFalco,我知道教一个新手关于这些概念(你已经掌握了)可能会觉得很麻烦。我能理解我的问题可能会偏离正轨。不过,我真诚地感谢你们的帮助。从今以后,我将尽量限制我的帖子。附言:我只是一个试图在技术先进的世界中生存的新手。