Link to home
Start Free TrialLog in
Avatar of jaxrpc
jaxrpc

asked on

C++ IPC implementations

Hi,

How do i implement IPC into my applications so my 2 independent applications may exchange data?
I program in both windows and linux so both ways of implementation is appreciated. The IPC i am looking at are Shared Memory map, sockets and pipes.

i googled and saw some use of ipc with <sys/ipc.h> etc may i know what kind of header file is this and other references about them? is it os independent? by the way i am using C/C++

thanks
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

The most portable is socket programming where there are only little differences between linux and windows (Windows: winsock2.h, closesocket, some constants, error codes). Sockets work on the same computer *and* in a network.

Shared memory is the fastest but the creation and accessing are different for windows and linux. In Windows it is memory mapped files. After the memory is established you could use a (self-written) class which manages the memory. As in both Linux and Windows you have a pointer to the shared storage the class can be made portable.

Pipes are portable as well but I have less expeiences using them.

Regards, Alex
   
Avatar of Deepu Abraham
Try this link:
http://www.codeproject.com/threads/Win32IPC.asp

Though it explains in windows, it gives you a good overview
Best Regards,
DeepuAbrahamK
ASKER CERTIFIED SOLUTION
Avatar of Deepu Abraham
Deepu Abraham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear friend i dnt know abut windows but u can get lots of help from begning linux programming 3rd edition.
u can use socket a client server approach for transfering data from one program to other using one way socket if u want both program exchand data then u need to create two way socket in yhose part u can do the processing as u want.

here are the code for refrence

/* client.c */
/*u need to pass the port number at run time which is taken as command line argument

#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include <stdio.h>                      /* NULL                */

#include <time.h>


int main(int argc,char *argv[])
{
      time_t now;

         int create_socket;

         int bufsize = 1024;

         char *buffer = malloc(bufsize);

         struct sockaddr_in address;
   

         if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)

           printf("The Socket was created\n");

         address.sin_family = AF_INET;

         address.sin_port = htons(9734);

         inet_pton(AF_INET,argv[1],&address.sin_addr);
   
         if (connect(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
           printf("The connection was accepted with the server %s...\n",inet_ntoa(address.sin_addr));
         do{
                  recv(create_socket,buffer,bufsize,0);

             now = time((time_t *)NULL);

                  printf("Time:%s",ctime(&now)," Message recieved:  %s",buffer);

                  if (strcmp(buffer,"/q"))

            {

                   printf("Message to send: ");

                   gets(buffer);

                   send(create_socket,buffer,bufsize,0);

     
            }

         }
      while (strcmp(buffer,"/q"));

         close(create_socket);

}





/* server.c */

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include <time.h>
#include<signal.h>
#include<fcntl.h>



int main()
{
      time_t now;
         int cont,create_socket,new_socket,addrlen,on,ret;
         int bufsize = 1024,res;
         char *buffer = malloc(bufsize);
         struct sockaddr_in address;
         struct linger li = { 0 };
      
      //int ret;
      int val;

         if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
      
      /* free the tcp port after closing the socket descriptor */
      li.l_onoff = 1;
      li.l_linger = 0;
      
      setsockopt( create_socket, SOL_SOCKET, SO_LINGER, (char *) &li,sizeof (struct linger));
      
      /* Make socket non-block. */
      val = fcntl(create_socket, F_GETFL, 0);
      //fcntl(create_socket, F_SETFL, val | O_NONBLOCK);
      
           printf("The socket was created\n");
         address.sin_family = AF_INET;
         address.sin_addr.s_addr = INADDR_ANY;
         address.sin_port = htons(9734);
         if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
           printf("Binding Socket\n");
           listen(create_socket,3);
      
         addrlen = sizeof(struct sockaddr_in);
         new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
         if (new_socket > 0)
      {
                  printf("The Client %s is connected...\n",inet_ntoa(address.sin_addr));
                  for(cont=1;cont<5000;cont++)
            printf("\x7");
         }
      else
      {
            perror("accept");
            exit(1);
      }
         do{
            now = time((time_t *)NULL);
                  printf(" Message send: %s",buffer);
                  gets(buffer);
                  send(new_socket,buffer,bufsize,0);
                  recv(new_socket,buffer,bufsize,0);
                  printf("Time Message recieved:%s %s\n",buffer,ctime(&now));
         }
      while(strcmp(buffer,"/q")); //user &#65533;q&#65533; to quit
         //close(new_socket);
      close(create_socket);
      
}


u can use these code this is a two way socket in which client and server both can send and recive message just modify them as per your requirement