版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yuewen2008/article/details/86375835
qnx进程间通讯的示例代码
msg_receive_server.c实现了 server端数据接收功能,并返回reply消息给client端。
msg_send_client.c实现了 client端的数据发送功能,将数据发送给server端,并等待server的reply消息。
server分别发送data 1和data2给client,client接收到data 1后返回replay消息 data3,client接收到data 2后返回replay消息 data 4.
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;
}