简体   繁体   English

不同尺寸的通用变量

[英]Universal variable for different sizes

I do some recv() and send() on sockets and I know the maximum packetSize is 256 MB .我在套接字上做了一些recv()send() ,我知道最大packetSize256 MB

Does it exist some variable which finds out the length of the packet and allocates that space?它是否存在一些变量来找出数据包的长度并分配该空间?

I mean我的意思是

  1. when packetSize is just 1045 B , allocate that sizepacketSize仅为1045 B ,分配该大小
  2. when packetSize is 56 MB , allocate that sizepacketSize56 MB ,分配该大小

Thanks!谢谢!

What you're looking for is std::vector .您正在寻找的是std::vector It can contain an arbitrary number of elements.它可以包含任意数量的元素。

Finds out the length of the packet找出包的长度

Are you sure you did not mean length of the message?你确定你不是指消息的长度? Also may depend on what protocol you use.也可能取决于您使用的协议。

Assuming a streaming protocol such as TCP, you can't really know until you receive the data.假设使用 TCP 等流协议,在收到数据之前您无法真正知道。 If your vector wasn't big enough, then resize and keep receiving until you have gotten all of it (or until you reach some safety limit to mitigate denial of service attacks).如果您的向量不够大,则调整大小并继续接收,直到您获得所有向量(或直到达到某个安全限制以减轻拒绝服务攻击)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM