简体   繁体   English

在同一解决方案中的两个项目之间通过 TCP 套接字实时流式传输 C++ 结构数据的最佳方法是什么?

[英]What is the best way for streaming C++ struct data through TCP socket in Real-time between two projects in the same solution?

Basically I have a struct:基本上我有一个结构:

typedef struct MyStruct1 {
  vector<MyStruct2> data;
  chrono::high_resolution_clock::time_point time_stamp;
} MyStruct1;

MyStruct2 is a struct that contains some opencv data such as: Point2f . MyStruct2是一个包含一些 opencv 数据的结构,例如: Point2f I want to share this between two projects that are in the same VS solution via TCP socket (because I need reliable connection).我想通过 TCP 套接字在同一个 VS 解决方案中的两个项目之间共享这个(因为我需要可靠的连接)。 Also I tried shared memory but figured out that is not a good way to share complex data structures but for POD.我也尝试过共享内存,但发现这不是共享复杂数据结构的好方法,但对于 POD。 But every time I dereference pointer to MyStruct1 in the client (project that receives data) my program crashes.但是每次我在客户端(接收数据的项目)中取消引用指向MyStruct1指针时,我的程序都会崩溃。 Is there any solution to this?有什么解决办法吗? Or should I try some other way (and what would that way be)?或者我应该尝试其他方式(那种方式是什么)? The code that I used is here: server and client (NOTE: I am not using argv and argc but everything else is the same).我使用的代码在这里: 服务器客户端(注意:我没有使用 argv 和 argc,但其他一切都是一样的)。 Thanks in advance.提前致谢。

As long as it is on the same machine and compiled at the same time with all settings the same and the data class is shared on both you can get away with defining an friend operator<< and operator>> for each type, that are to be send.只要它在同一台机器上并在所有设置相同的同时编译并且数据类在两者上共享,您就可以为每种类型定义一个朋友operator<<operator>> ,即被发送。

Totally untested incomplete code完全未经测试的不完整代码

ostream& operator<<(ostream& os, const MyStruct1 & dt) {
    os << dt.time_stamp << dt.data.size(); // time_stamp might need more magic
    for(auto& val : dt.data) // the MyStruct2 must also have an <<
      os << val;
    return os;
}

Write it to a strstream and send the string to the other side, decode it with the >> .将其写入 strstream 并将字符串发送到另一端,使用>>对其进行解码。

TCP provides a stream of bytes. TCP 提供字节流。 To send something over TCP, you need to serialize it into a stream of bytes.要通过 TCP 发送某些内容,您需要将其序列化为字节流。 You can't just write the MyStruct1 object over the TCP connection.您不能只通过 TCP 连接编写MyStruct1对象。 That will never work correctly because the object's internal representation in memory will contain data meaningful only inside the process and that will mean nothing to another process.这永远不会正常工作,因为内存中对象的内部表示将包含仅在进程内部有意义的数据,而这对另一个进程没有任何意义。

Another issue to keep in mind: There might be one entry in that vector.要记住的另一个问题:该向量中可能有一个条目。 There might be a thousand.可能有一千个。 How will the receiver know how many bytes to read or know where the object ends?接收者如何知道要读取多少字节或知道对象在哪里结束?

There are lots of libraries and tools for serialization and deserialization.有很多用于序列化和反序列化的库和工具。

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

相关问题 什么是在实时应用程序中同步多个线程之间的容器访问的最佳方法 - what is the best way to synchronize container access between multiple threads in real-time application 具有磁盘持久性的最佳存储/缓存实时数据的方法 - Best way of storing/caching real-time data , with disk persistency 如何在Java和C ++代码之间传递实时数据? - How to communicate real-time data between Java and C++ code? 什么是同时学习C ++和Qt的最佳方法? - what's the best way to learn C++ and Qt at the same time? 从 C/C++ 中的 TCP 套接字读取的正确方法是什么? - What is the correct way of reading from a TCP socket in C/C++? 在C ++中通过tcp套接字发送struct - Sending struct over tcp socket in C++ 在c ++中,在线程之间共享数据容器的最佳方法是什么 - What is the best way to share data containers between threads in c++ 什么是Android上实时图形编程的最佳语言? - What's the best language for real-time graphics programming on Android? GoogleTest在同一解决方案中几个C ++项目 - GoogleTest several C++ projects in the same solution 在 C++ 中初始化位域结构的最佳方法是什么? - What is the best way to initialize a bitfield struct in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM