如何使用C+&#x2B中的Indy TCP客户端编写原始二进制数据建筑商
使用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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,
tidstream
在很长一段时间内都不是Indy的一部分,这让我想知道您是否正在使用Indy的旧版本,而不是使用C ++构建器发货的版本10.3 。 Indy很长一段时间以来支持RTL的标准Tstream
类。话虽如此...
tidbytes
是system :: DynamIcarray< system :: byte>
的别名,其中system :: byte
是一个无符号char
的别名,与uint8_t
相同的大小和签名(根据编译器,uint8_t
,甚至可能只是一个别名unsigned char
)。因此,最简单的解决方案,无需必须单独的数据副本,就是简单地将其键入键入,例如:
这在技术上是 nordefined行为,因为
dynamicArray< uint8_t>
anddynamiCarray< byte>
是无关类型(除非uint8_t
and code> byte byte 是> code>> byte
byte 未签名的char ),但是在您的情况下它将起作用,因为它是两个数组背后的基础代码,uint8_t
andbyte
具有相同的基础内存布局。另外,下一个最简单的解决方案,,没有复制数据或调用未定义的行为,是使用Indy的
tidreadonlymemorybufferstream
iniDglobal.hpp.hpp.hpp
,EG:或:
否则,最终解决方案是将数据复制到
tidbytes
,例如: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 standardTStream
class for a very long time.That being said...
TIdBytes
is an alias forSystem::DynamicArray<System::Byte>
, whereSystem::Byte
is an alias forunsigned char
, which is the same size and sign-ness asuint8_t
(depending on compiler,uint8_t
might even just be an alias forunsigned char
).So, the simplest solution, without having to make a separate copy of your data, is to simply type-cast it, eg:
This is technically undefined behavior, since
DynamicArray<uint8_t>
andDynamicArray<Byte>
are unrelated types (unlessuint8_t
andByte
are both aliases forunsigned char
), but it will work in your case since it is the same underlying code behind both arrays, anduint8_t
andByte
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 inIdGlobal.hpp
, eg:Or:
Otherwise, the final solution is to just copy the data into a
TIdBytes
, eg: