2

I'm having so many doubts about this that i thought i should ask. Here's the situation:

I'm writing a C++ .dll that is going to be loaded into a program,"EuroScope.exe". While loaded, the program will give data to the dll which will be constantly sending and receiving data to/from a previously built python program. After receveiving and accepting the data, the dll will transmit it to the running application

I was trying the sockets solution with ZeroMQ that everyone talked so greatly about. Thing is, it's been 2 weeks now and i can't put it to work in VS2012. I tried everything, even opening a thread here,in their homesite,in the mailing list...no one knows how to solve it

So, besides ZeroMQ what do you think is the best option in my case? I already searched and read a lot of them out there (protocol buffers i don't like, it's too complicated for what i want to do i think)

1 more thing. Is binding Python/C++ (e.g. with SIP or ctypes) a valid solution or it doens't serve my purposes? I've read some SIP and ctypes documentation and it seems like what i want...but at the same time i think: what's the purpose of having a C++ program accessible to Python if it's the loaded dll that needs to send data to Python?? I don't know,i'm just overwhelmed with so many information

Thank you very much for the help

4

1 回答 1

0

最简单的方法之一是编写一个简单的 Python 套接字服务器,它执行以下操作:

  1. 侦听已知端口并接受 TCP 连接
  2. 读取一个字符串,直到找到一个 '\n'
  3. 在某些分隔符(例如 ',' 或空格(例如 flds = line.split(','))上拆分行
  4. 将第一个字段 (flds[0]) 解释为关键字,将其余字段解释为参数以调用底层 python 程序中的函数
  5. 发送以 '\n' 结尾的响应字符串
  6. 继续循环步骤 2 到 5,直到套接字连接关闭或收到某种“CLOSE”命令。

此类服务器的客户端可以用 C++ 或 Python 编写,并且可以:

  1. 连接到已知端口上的 TCP 套接字
  2. 发送以 '\n' 结尾的单行命令
  3. 读取响应字符串,直到找到 '\n'
  4. 重复上述过程,直到所需任务完成或socket连接关闭

当我需要一个简单的点对点同步发送/接收协议实现时,我经常使用这种方法。与 ZeroMQ 或其他消息队列方法相比,它的重量级要小得多。

如果您需要异步通信或有多个服务器线程正在运行,那么您可能需要研究 ZeroMQ 之类的东西。但是,如果我理解正确的话,在这种情况下,您的需求听起来要简单得多。

在网络上使用字符串比发送二进制数据要慢一些,但根据我的经验,该协议更容易调试,因为您不必担心二进制类型(int、long、short、 float, double) 跨不同平台。而且您不必将数据包转储到文件中并在 HEX 编辑器中查看它们以了解通过网络发送的内容。只需记录文本字符串,就很容易看到发生了什么。

Python 在标准库中具有出色的TCP 套接字服务器和客户端库支持,并且 .NET 库具有良好的 TcpClient 支持,如果您决定使用 C# 编写客户端或决定使用托管 C++,它将很好地工作。

如果您要编写更传统的 C++(非托管),那么您应该能够找到大量 Win32 套接字编程示例,例如这个带有工作 TCP 客户端和服务器 Win32 套接字代码示例和在线书籍的站点

于 2013-07-15T03:58:52.327 回答