Pho*_*rce 3 c++ python sockets
我主要在这里寻求一些建议。
我正在开发一个应用程序,其中主要处理(存储在服务器上)是用 C++ 执行的,GUI(前端)是用 Python 执行的。这两个程序将相互通信。Python 将发送 C++ 程序工作所需的文件,并为 C++ 程序提供一些要处理的数据。然后后端将与处理后的数据进行通信。
因此使用套接字会更好吗?我想过使用文本文件来完成此操作,但是,已经放弃了这个想法,而是将数据保存为 .txt 文件,以便在将来的情况下可以打开它。另外,如果我使用套接字,使用Python/C++会有冲突吗?
尝试ZeroMQ
\n\n\n\n\n\xc3\x98MQ(也称为 ZeroMQ、0MQ 或 zmq)看起来像一个可嵌入的网络库,但其作用却像一个并发框架。它为您提供了跨进程内、进程间、TCP 和多播等各种传输方式传送原子消息的套接字。您可以使用扇出、发布-订阅、任务分配和请求-答复等模式连接 N 对 N 的套接字。它的速度足够快,可以成为集群产品的结构。其异步 I/O 模型为您提供可扩展的多核应用程序,构建为异步消息处理任务。它具有多种语言 API,并可在大多数操作系统上运行。\xc3\x98MQ 来自\n iMatix,是 LGPLv3 开源的。
\n
C++ 你好世界服务器:
\n\n//\n// Hello World server in C++\n// Binds REP socket to tcp://*:5555\n// Expects "Hello" from client, replies with "World"\n//\n#include <zmq.hpp>\n#include <string>\n#include <iostream>\n#ifndef _WIN32\n#include <unistd.h>\n#else\n#include <windows.h>\n#endif\n\nint main () {\n // Prepare our context and socket\n zmq::context_t context (1);\n zmq::socket_t socket (context, ZMQ_REP);\n socket.bind ("tcp://*:5555");\n\n while (true) {\n zmq::message_t request;\n\n // Wait for next request from client\n socket.recv (&request);\n std::cout << "Received Hello" << std::endl;\n\n // Do some \'work\'\n#ifndef _WIN32\n sleep(1);\n#else\n Sleep (1);\n#endif\n\n // Send reply back to client\n zmq::message_t reply (5);\n memcpy ((void *) reply.data (), "World", 5);\n socket.send (reply);\n }\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\nPython客户端:
\n\n#\n# Hello World client in Python\n# Connects REQ socket to tcp://localhost:5555\n# Sends "Hello" to server, expects "World" back\n#\nimport zmq\n\ncontext = zmq.Context()\n\n# Socket to talk to server\nprint "Connecting to hello world server\xe2\x80\xa6"\nsocket = context.socket(zmq.REQ)\nsocket.connect("tcp://localhost:5555")\n\n# Do 10 requests, waiting each time for a response\nfor request in range(10):\n print "Sending request %s \xe2\x80\xa6" % request\n socket.send("Hello")\n\n # Get the reply.\n message = socket.recv()\n print "Received reply %s [ %s ]" % (request, message)\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
4278 次 |
| 最近记录: |