Full width home advertisement

Electronics Hub

Internet of Networks

Post Page Advertisement [Top]

Q. Implement Client-Server Communication by using IPC Message Queue in C++.


Ans.

Before we implement the client-server communication in C++ by IPC messaging queue, we need to know what is IPC Meesage Queue.

Earlier in this website, you have seen How Client-Server Communication can be established by using TCP Socket in JAVA.

But now, you are going to have another taste of client-server communication not in JAVA but in C++ 


Free picture downloaded from unsplash.com


IPC Message Queue


A message queue can be defined as a linked list of messages can be stored within the kernel (basic level of operating system, responsible for resource allocation, file management) and recognised by message queue identifier.

All the processes in an operating system can exchange information by accessing a common system-based message queue. The sending process takes places by placing a message (via message-passing model) into the queue which can be read by any other process. Each messages are given an identification, so that the processes can select the appropriate message for it. However, the processes must share an universal key to access the queue the earliest.

This communication between processes are known as Inter-Process Communication or IPC. 

Lets see the picture below how IPC message queue works, 


ipc message, ipc message queue
IPC Message Queue

Lets, come to the socket programming of how the client server communication can be implemented by using IPC Message Queue in C++.

Server side program:

#include<sys/types.h>
#include<sys/ipc.h>
#include<stdio.h>
#include<sys/msg.h>
#include<string.h>
main()
{
int qid;
struct
{
long mtype;
char mtext[25];
}
message;
qid=msgget((key-t)10,IPC_CREAT 10666);
if(qid==-1)
{
penor("mesgget failed");
return 1;
}
strcpy(message.mtext,"Good Morning\n");
message.mtype=1;

if(msgsnd(qid,&message,strlen(message.mtext),0==-1))
{
penor("msgsnd failed\n");
return 1;
}
else
printf("Successfully sent\n");
}

Client Side Program:

#include<sys/types.h>
#include<sys/ipc.h>
#include<stdio.h>
#include<sys/msg.h>
main()
{
int qid,t,key;
struct
{
long mtype;
char mtext[25];
}
buff;
qid=msgget((key-t)10,IPC_CREAT 10666);
if(qid==-1)
{
penor("mesgget failed");
return 0;
}
printf("message received %s\n",buff,mtext);
}

First execute server side program,

Output:

$ gcc msg.c (//enter)

    ./a.out (//enter)

Successfully sent


Then execute client side program,

Output:

$ gcc msg1.c (//enter)

    ./a.out (//enter)

message received Good Morning

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Sriparna Sarkar