2

I want to send a vector from one computer to another through internet. I'm looking for a Peer-to-Peer solution in C++. I have made a Winsock2 solution, but it can only send char* with the send and recv-functions which I don't think will work for my project.

Is there a way of using JSON with a P2P-solution in C++? So make a JSON-object of the vector and send it through internet? Or do you know a better solution?

The vector I want to send through internet to another computer looks like this:

Vector<AVpacket> data;

AVpacket is a struct from ffmpeg, consisting 14 data members. https://ffmpeg.org/doxygen/trunk/structAVPacket.html. You don't want to make this to a char*

Kara
  • 6,115
  • 16
  • 50
  • 57
Fredde
  • 23
  • 3
  • related: http://stackoverflow.com/questions/523872/how-do-you-serialize-an-object-in-c – eerorika Apr 15 '14 at 11:06
  • This could be useful to you: http://stackoverflow.com/questions/1577161/passing-a-structure-through-sockets-in-c –  Apr 15 '14 at 11:16

2 Answers2

4

You can actually send anything using the send and recv functions. You just have to make sure you pass a pointer to the data, and then typecast that pointer as a char * and it will work.

However, you can't send a std::vector as is. Instead you should first send its size (otherwise the receiving end will not know how much data it should receive) then you send the actual data in the vector, i.e. someVector.data() or &someVector[0].

Though in your case it will be even more complicated, as the structures you want to send contains embedded pointers. You can't send pointers over the Internet, it's barely possible to transfer pointers between two processes on the same system. You need to read about serialization and maybe about the related subject marshalling.

In short: You can send any kind of data, it doesn't have to be characters, and for the kind of structures you want to send you have to convert them to a format is transferable through serialization.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You can not simply send a vector. You can not be sure how the std allocator reserved the memory. So it is very likely that the whole memory of the vector is not just one big linear chunk.

In addition to this, as pointed out above, there are pointers in this struct. They point to data in your local memory. These addresses aren't valid on the recipients side, thus you would access invalid memory trying to read this.

I guess, that in order to achieve what you want to do, you have to try a completely different approach. Do not get lost by trying to send the data of the pointers or similar, rather try having parallel data on both machine.

E.g. both load the same video e.g. from a file which both share. Then use a unique identifier for that video to reference the video on both sides.

meandbug
  • 202
  • 2
  • 5