Reputation: 4642
I'm looking for some advice mainly here.
I'm working on an application, where the main processing (stored on a server) is carried out in C++ and the GUI (front-end) is carried out in Python. These two programs will communicate with each other. The Python will send across the files needed for the C++ program to work, and give the C++ program some data to work with.. The back-end will then communicate back with the processed data.
Would therefore it be better to use Sockets? I thought about completing this using text files, but, have gone off this idea, instead will just save the data as a .txt file so it can be opened up in future instances. Also, if I was to use sockets, would there be any conflict in using Python/C++?
Upvotes: 3
Views: 4501
Reputation: 11
Updated Solution: The python code is nearly identical, with the addition of "()" brackets on the print statements and use of "socket.send_string", not "socket.send". The c++ code was taken from a zmq example. For me, this solution is working well on Windows 11, c++20, vs2022, and python 3.9. https://zeromq.org/get-started/?language=cpp&library=cppzmq#
c++ code:
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.hpp>
#include <string>
#include <iostream>
using namespace std::chrono_literals;
int main() {
// initialize the zmq context with a single IO thread
zmq::context_t context{ 1 };
// construct a REP (reply) socket and bind to interface
zmq::socket_t socket{ context, zmq::socket_type::rep };
socket.bind("tcp://*:5555");
// prepare some static data for responses
const std::string data{ "World" };
for (;;)
{
zmq::message_t request;
// receive a request from client
socket.recv(request, zmq::recv_flags::none);
std::cout << "Received " << request.to_string() << std::endl;
// simulate work
std::this_thread::sleep_for(1s);
// send the reply to the client
socket.send(zmq::buffer(data), zmq::send_flags::none);
}
return 0;
}
python code:
# Hello World client in Python
# Connects REQ socket to tcp://localhost:5555
# Sends "Hello" to server, expects "World" back
#
import zmq
context = zmq.Context()
# Socket to talk to server
print ("Connecting to hello world server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
# Do 10 requests, waiting each time for a response
for request in range(10):
print ("Sending request %s …" % request)
socket.send_string("Hello")
# Get the reply.
message = socket.recv()
print ("Received reply %s [ %s ]" % (request, message))
Upvotes: 0
Reputation: 14360
Would therefore it be better to use Sockets?
When working on network you will be always using sockets at the end. We can say the sockets are the core of every networking application. Said this, in answer to your question, I think your application is quite simple (as you describe it) so it is better don't get complicated using some third party module or even an entire framework to do this.
See this answer Python - Sending files over sockets And this to c c send and receive file
I hope that help you.
Upvotes: 0
Reputation: 1651
I would go for named pipes, would be readily available in your circumstance because it is just similar to reading and writing to file, but it also has features similar to sockets i.e. you can make them communicate on a network (different hosts)
Upvotes: 0
Reputation: 22561
Try ZeroMQ
ØMQ (also known as ZeroMQ, 0MQ, or zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry atomic messages across various transports like in-process, inter-process, TCP, and multicast. You can connect sockets N-to-N with patterns like fan-out, pub-sub, task distribution, and request-reply. It's fast enough to be the fabric for clustered products. Its asynchronous I/O model gives you scalable multicore applications, built as asynchronous message-processing tasks. It has a score of language APIs and runs on most operating systems. ØMQ is from iMatix and is LGPLv3 open source.
C++ Hello world server:
//
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.hpp>
#include <string>
#include <iostream>
#ifndef _WIN32
#include <unistd.h>
#else
#include <windows.h>
#endif
int main () {
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
socket.bind ("tcp://*:5555");
while (true) {
zmq::message_t request;
// Wait for next request from client
socket.recv (&request);
std::cout << "Received Hello" << std::endl;
// Do some 'work'
#ifndef _WIN32
sleep(1);
#else
Sleep (1);
#endif
// Send reply back to client
zmq::message_t reply (5);
memcpy ((void *) reply.data (), "World", 5);
socket.send (reply);
}
return 0;
}
Python client:
#
# Hello World client in Python
# Connects REQ socket to tcp://localhost:5555
# Sends "Hello" to server, expects "World" back
#
import zmq
context = zmq.Context()
# Socket to talk to server
print "Connecting to hello world server…"
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
# Do 10 requests, waiting each time for a response
for request in range(10):
print "Sending request %s …" % request
socket.send("Hello")
# Get the reply.
message = socket.recv()
print "Received reply %s [ %s ]" % (request, message)
Upvotes: 5