如何使用C+&#x2B中的Indy TCP客户端编写原始二进制数据建筑商

发布于 2025-01-27 05:09:24 字数 477 浏览 2 评论 0 原文

使用Embarcadero C ++构建器10.3。

我有一个 dynamicArray< uint8_t> mydata 对象。我想使用 tidtcpclient 组件将其原始二进制内容(字节)发送到服务器。我是这样要做的:

TIdTcpClient tcpClient1;
// Bla Bla Bla
tcpClient1->IOHandler->Write(rawData);

其中 rawdata 应该是类型 tidbytes tidstream

因此基本上归结为以下内容:要将 mydata 对象转换为 rawdata 类型 tidbytes tidstream

Using Embarcadero C++ Builder 10.3.

I have a DynamicArray<uint8_t> myData object. I want to send/write its raw binary content (bytes) to a server using the TIdTcpClient component. I'm going about it like this:

TIdTcpClient tcpClient1;
// Bla Bla Bla
tcpClient1->IOHandler->Write(rawData);

Where rawData should be of type TIdBytes or TIdStream

So basically, it boils down to the following: How to convert myData object to a rawData type of either TIdBytes or TIdStream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2025-02-03 05:09:24

首先, tidstream 在很长一段时间内都不是Indy的一部分,这让我想知道您是否正在使用Indy的旧版本,而不是使用C ++构建器发货的版本10.3 。 Indy很长一段时间以来支持RTL的标准 Tstream 类。

话虽如此...

tidbytes system :: DynamIcarray&lt; system :: byte&gt; 的别名,其中 system :: byte 是一个无符号char 的别名,与 uint8_t 相同的大小和签名(根据编译器, uint8_t ,甚至可能只是一个别名 unsigned char )。

因此,最简单的解决方案,无需必须单独的数据副本,就是简单地将其键入键入,例如:

tcpClient1->IOHandler->Write(reinterpret_cast<TIdBytes&>(myData));

这在技术上是 nordefined行为,因为 dynamicArray&lt; uint8_t&gt; and dynamiCarray&lt; byte&gt; 是无关类型(除非 uint8_t and code> byte byte 是> code>> byte byte 未签名的char ),但是在您的情况下它将起作用,因为它是两个数组背后的基础代码, uint8_t and byte 具有相同的基础内存布局。

另外,下一个最简单的解决方案,,没有复制数据或调用未定义的行为,是使用Indy的 tidreadonlymemorybufferstream in iDglobal.hpp.hpp.hpp ,EG:

TIdReadOnlyMemoryBufferStream *ms = new TIdReadOnlyMemoryBufferStream(&myData[0], myData.Length);
try {
    tcpClient1->IOHandler->Write(ms);
}
__finally {
    delete ms;
}

或:

{
auto ms = std::make_unique<TIdReadOnlyMemoryBufferStream>(&myData[0], myData.Length);
tcpClient1->IOHandler->Write(ms.get());
}

否则,最终解决方案是将数据复制到 tidbytes ,例如:

{
TIdBytes bytes;
bytes.Length = myData.Length;

memcpy(&bytes[0], &myData[0], myData.Length);
or:
std::copy(myData.begin(), myData.end(), bytes.begin());

tcpClient1->IOHandler->Write(bytes);
}

First off, TIdStream has not been part of Indy in a VERY VERY LONG time, which makes me wonder if you are using a very old version of Indy, not the one that shipped with C++Builder 10.3. Indy has supported the RTL's standard TStream class for a very long time.

That being said...

TIdBytes is an alias for System::DynamicArray<System::Byte>, where System::Byte is an alias for unsigned char, which is the same size and sign-ness as uint8_t (depending on compiler, uint8_t might even just be an alias for unsigned char).

So, the simplest solution, without having to make a separate copy of your data, is to simply type-cast it, eg:

tcpClient1->IOHandler->Write(reinterpret_cast<TIdBytes&>(myData));

This is technically undefined behavior, since DynamicArray<uint8_t> and DynamicArray<Byte> are unrelated types (unless uint8_t and Byte are both aliases for unsigned char), but it will work in your case since it is the same underlying code behind both arrays, and uint8_t and Byte have the same underlying memory layout.

Alternatively, the next simplest solution, without copying data or invoking undefined behavior, is to use Indy's TIdReadOnlyMemoryBufferStream class in IdGlobal.hpp, eg:

TIdReadOnlyMemoryBufferStream *ms = new TIdReadOnlyMemoryBufferStream(&myData[0], myData.Length);
try {
    tcpClient1->IOHandler->Write(ms);
}
__finally {
    delete ms;
}

Or:

{
auto ms = std::make_unique<TIdReadOnlyMemoryBufferStream>(&myData[0], myData.Length);
tcpClient1->IOHandler->Write(ms.get());
}

Otherwise, the final solution is to just copy the data into a TIdBytes, eg:

{
TIdBytes bytes;
bytes.Length = myData.Length;

memcpy(&bytes[0], &myData[0], myData.Length);
or:
std::copy(myData.begin(), myData.end(), bytes.begin());

tcpClient1->IOHandler->Write(bytes);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文