user8580
user8580

Reputation: 13

send and receive data between 2 application in c++

I want to write two c++ application the first one called "caller," the second called "processor."

caller application send a value to a processor. after that processor do some process on that value and return it back to caller.

I want to know the best way to do that.

so far I know how to send value from caller application to processor application but I could not find any way to return the value back. one of this way by using putenv() and getenv()

caller example

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv, char** envp){
    char myvar[]="MYVAR= say something";
    putenv(myvar);
    cout<<getenv("MYVAR")<<endl;
    system("./processor");
    cout<<getenv("MYVAR")<<endl;
}

processor example

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv, char** envp){
    char myvar[]="MYVAR= say something else";
    putenv(myvar);
    cout<<getenv("MYVAR")<<endl;
}

how to fix that to return value from processor to caller and if there is better way what is it. I am using Ubunut OS.

Upvotes: 1

Views: 798

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

There are many possible ways to communicate between to processes, and which is best depends on a lot of factors. Using the environment is basically only good for one-way communication, not bidirectional communication. The methods available include:

  • Environment variables (one way)
  • Command-line arguments (one way)
  • Pipes
  • Files
  • Sockets
  • FIFOs
  • Shared memory
  • Message queues
  • Signals
  • Semaphores

Some are limited to processes connected by a common ancestor; others can work between unrelated processes, sometimes not even on the same machine. Some mechanisms can communicate very little data (signals, for example); others can communicate large quantities of data. Some require no synchronization; others require extensive synchronization.

Which is best for you? We don't have enough information to guess. However, your best choice is most likely between pipes, files and sockets.

Upvotes: 2

Related Questions