Is it possible to send an array over the network?

I use C ++ and wonder if I can just send the whole int array over the network (using basic sockets) without doing anything. Or do I need to split the data and send it one at a time?

+3
source share
2 answers

You can definitely send the array in one send, however you may need to do some extra work. There are problems with its correct interpretation on the receiving side. For example, if you use different computer architectures, you may need to convert integers to network order (for example, htonl).

Another thing to keep in mind is the memory layout. If it is a simple array of integers, then it will be contiguous in memory, and one send can successfully capture all the data. If, although (and this is probably obvious), you have an array with other data, then the layout definitely needs to be considered. A simple example would be if there were pointers to other data in the array, such as a character string, then sending the array would send pointers (not data) and would be pointless to the recipient.

+2
source

Yes.

The array will be sequentially written to memory so you can do this. Just go to the address of the first item and the amount of data, and you will send all the data.

+3
source

Source: https://habr.com/ru/post/1741879/


All Articles