是否可以通过网络发送数组?

发布于 2024-08-30 04:55:52 字数 77 浏览 4 评论 0原文

我正在使用 C++,想知道是否可以通过网络(使用基本套接字)发送整个 int 数组而不执行任何操作。或者我是否必须将数据分开并一次发送一个?

I'm using C++ and wondering if I can just send an entire int array over a network (using basic sockets) without doing anything. Or do I have to split the data up and send it one at a time?

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

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

发布评论

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

评论(2

关于从前 2024-09-06 04:55:52

是的。

数组将在内存中按顺序排列,因此您可以自由地执行此操作。只需传入第一个元素的地址和数据量,您就可以发送所有数据。

Yes.

An array will be laid out sequentially in memory so you are free to do this. Simply pass in the address of the first element and the amount of data and you'll send all data.

挽容 2024-09-06 04:55:52

您绝对可以在一次发送中发送一个数组,但是您可能需要做一些额外的工作。在接收端正确解释它存在问题。例如,如果使用不同的机器架构,您可能需要将整数转换为网络顺序(例如,htonl)。

另一件要记住的事情是内存布局。如果它是一个简单的整数数组,那么它将在内存中是连续的,并且单个发送可以成功捕获所有数据。但是,如果(这可能是显而易见的)您有一个包含其他数据的数组,那么布局肯定需要考虑。一个简单的例子是,如果数组具有指向其他数据(例如字符串)的指针,那么数组的发送将发送指针(而不是数据),并且对接收者来说毫无意义。

You could definitely send an array in one send, however you might want to do some additional work. There are issues with interpreting it correctly at the receiving end. For example, if using different machine architectures, you may want to convert the integers to network order (e.g., htonl).

Another thing to keep in mind is the memory layout. If it is a simple array of integers, then it would be contiguous in memory and a single send could successfully capture all the data. If, though, (and this is probably obvious), you have an array with other data, then the layout definitely needs consideration. A simple example would be if the array had pointers to other data such as a character string, then a send of the array would be sending pointers (and not data) and would be meaningless to the receiver.

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