Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/yuewen2008/article/details/86375835
Sample Code qnx inter-process communication
msg_receive_server.c implements the server side data reception function, and returns the reply message to the client.
msg_send_client.c implements client-side functionality of data transmission, transmits the data to the server side and waits for a reply message server.
data server. 1 and data2 are transmitted to client, client to the data received replay message returned after data3 1, client receives a return message data. 4 replay after 2 data.
msg_send_client.c
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dispatch.h>
#define ATTACH_POINT "myname"
/* We specify the header as being at least a pulse */
typedef struct _pulse msg_header_t;
/* Our real data comes after the header */
typedef struct _my_data {
msg_header_t hdr;
int data;
} my_data_t;
/*** Client Side of the code ***/
int client()
{
my_data_t msg;
int msg_reply;
int server_coid;
if ((server_coid = name_open(ATTACH_POINT, 0)) == -1) {
printf("client name open failed\n");
return EXIT_FAILURE;
}
/* We would have pre-defined data to stuff here */
msg.hdr.type = 0x00;
msg.hdr.subtype = 0x00;
msg.data = 1;
/* Do whatever work you wanted with server connection */
printf("client name open success, Client sending msg %d \n", msg.data);
if (MsgSend(server_coid, &msg, sizeof(msg), &msg_reply, sizeof(msg_reply)) == -1) {
printf("client send msg 1 error\n");
}
printf("client receive msg 1 reply: %d \n", msg_reply);
msg.hdr.type = 0x00;
msg.hdr.subtype = 0x01;
msg.data = 2;
printf("client name open success, Client sending msg %d \n", msg.data);
if (MsgSend(server_coid, &msg, sizeof(msg), &msg_reply, sizeof(msg_reply)) == -1) {
printf("client send msg 2 error\n");
}
printf("client receive msg 2 reply: %d \n", msg_reply);
/* Close the connection */
name_close(server_coid);
return EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
int ret;
if (argc < 2) {
printf("Usage %s -s | -c \n", argv[0]);
ret = EXIT_FAILURE;
}
else if (strcmp(argv[1], "-c") == 0) {
printf("Running client ... \n");
ret = client();
}
else {
printf("Usage %s -s | -c \n", argv[0]);
ret = EXIT_FAILURE;
}
return ret;
}
msg_receive_server.c
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dispatch.h>
#define ATTACH_POINT "myname"
/* We specify the header as being at least a pulse */
typedef struct _pulse msg_header_t;
/* Our real data comes after the header */
typedef struct _my_data {
msg_header_t hdr;
int data;
} my_data_t;
int msg_update_fail =3;
int msg_update_success =4;
/*** Server Side of the code ***/
int server() {
name_attach_t *attach;
my_data_t msg;
my_data_t msg_reply;
msg_reply.hdr.type = 0x00;
msg_reply.hdr.subtype = 0x00;
// my_data_t msg_replaydata;
int rcvid;
/* Create a local name (/dev/name/local/...) */
if ((attach = name_attach(NULL, ATTACH_POINT, 0)) == NULL) {
printf("server name_attach error\n");
return EXIT_FAILURE;
}
printf("server name_attach suceess,wait masg from client\n");
/* Do your MsgReceive's here now with the chid */
while (1) {
rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL);
if (rcvid == -1) {/* Error condition, exit */
break;
}
/* A message (presumable ours) received, handle */
switch(msg.data){
case 1:
printf("Server receive msg data %d \n", msg.data);
MsgReply(rcvid, EOK, &msg_update_fail, sizeof(msg_update_fail));
//MsgReply(UpdateReceiveId, EOK, &msg_update_fail, 0);
break;
case 2:
printf("Server receive msg data %d \n", msg.data);
MsgReply(rcvid, EOK, &msg_update_success, sizeof(msg_update_success));
break;
default:
break;
}
MsgReply(rcvid, EOK, 0, 0);
}
/* Remove the name from the space */
name_detach(attach, 0);
return EXIT_SUCCESS;
}
int main(int argc, char **argv) {
int ret;
if (argc < 2) {
printf("Usage %s -s | -c \n", argv[0]);
ret = EXIT_FAILURE;
}
else if (strcmp(argv[1], "-s") == 0) {
printf("Running Server ... \n");
ret = server();
}
else {
printf("Usage %s -s | -c \n", argv[0]);
ret = EXIT_FAILURE;
}
return ret;
}