【问题标题】:How to send ostream by socket in C++?如何在 C++ 中通过套接字发送 ostream?
【发布时间】:2019-05-25 01:58:13
【问题描述】:

我正在尝试通过 C++ 中的套接字发送包含二进制数据的 ostream

我正在使用Microsoft SEAL 关于加密的发展。
现在我需要从客户端向服务器发送一个密文。 Microsoft SEAL 提供了一个函数

void Ciphertext::save(ostream &stream) const

Ciphertext对象序列化为ostream,但是在调用

void Ciphertext::save(ostream &stream) const之后,

我不知道如何在客户端通过套接字发送密文(在ostream)(当然也不知道如何在服务器中接收)。

我试过solution,但是当数据是二进制数据的时候好像不行。

我想知道如何编写在客户端发送ostream 并在服务器端接收的代码。谢谢。

【问题讨论】:

  • 您可以编写(或找到)一个自定义的std::streambuf 类来包装您的套接字,然后您可以将该类分配给标准std::ostream,从而允许save() 直接输出(输入)到你的插座。

标签: c++ sockets ostream


【解决方案1】:

您可以传递ostringstream 来填充并从中提取包含二进制数据的字符串:

#include <sstream>
#include <string>
// ...
std::string save_into_string(Ciphertext const& ciphertext) {
    std::ostringstream s;
    ciphertext.save(s);
    return s.str();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-29
    • 2012-12-18
    • 2013-09-02
    • 2012-01-20
    • 2013-03-24
    • 2015-01-02
    • 2013-07-22
    相关资源
    最近更新 更多