【发布时间】:2020-05-27 17:03:08
【问题描述】:
我有一个 std::stringstream 类型的变量,我在其中以 blob 格式写入数据,之后我想将其发送到管道,从这里数据被另一个应用程序接管并通过 tcp 套接字发送。当我调用 std::stringstream.str() 时,它会向我显示正确的信息。如果我给 std::cout
std::stringstream stream;
int fd;
const char* fifo = "/tmp/adpluspipe";
/* create the FIFO (named pipe) */
mkfifo(fifo, 0666);
/* write data to the FIFO */
fd = open(fifo, O_WRONLY);
write(fd, stream.str().data(), 84);
close(fd);
/* remove the FIFO */
unlink(fifo);
流变量的内容就是图片中的那个: content of the stream variable
我通过套接字接收一串字节,根据加密文档对该内容进行解码,应用一些过滤器,然后仅使用我需要的数据对其进行编码。它有很多带有'\0' 的区域,因为最终它必须具有相同的长度,以便在应用解码文档时它可以工作。代码很大很复杂,所以我无法显示它。该编码内容看起来像图片中的内容,我必须通过管道将其发送到另一个应用程序。我使用了std::stringstream,因为在这里我可以根据加密文档逐字节添加。
该变量中的写入代码是:
// The data is added like this:
int value = std::bitset<8>("00111101").to_ulong();
// or
int value = 80;
// and so on
void writeBytes(std::stringstream &stream, int value, int valueSize)
{
unsigned char bytes[valueSize];
if(valueSize == 1){
bytes[0] = value & 0xFF;
}
if(valueSize == 2){
bytes[0] = (value >> 8) & 0xFF;
bytes[1] = value & 0xFF;
}
if(valueSize == 4){
bytes[0] = (value >> 24) & 0xFF;
bytes[1] = (value >> 16) & 0xFF;
bytes[2] = (value >> 8) & 0xFF;
bytes[3] = value & 0xFF;
}
stream.write(reinterpret_cast<char*>(&bytes), sizeof(bytes));
memset(bytes, 0, sizeof(bytes));
}
【问题讨论】:
-
请不要发布指向文本图像的链接。请在您的问题中将文本作为文本发布。流变量的内容是图像中显示的文本吗?或者,当使用流变量的内容进行管道传输时,图像是否表示执行
hexdump或类似程序的结果文本? -
您没有在管道中写入任何内容,而读者应该是制造和破坏 fifo 的人。如果您取消链接 fifo,作者应该如何阅读?
-
图像包含流变量的内容。我想通过管道发送该内容,然后可以对其进行解码。如果我在流变量中放置一个字符串而不是那个内容,然后通过管道发送它,我可以毫无问题地读取该文本。
-
也请提供Minimal, Reproducable Example。否则,我们通常无法帮助您。
标签: c++