int msgget(key_t, key, int msgflg);
int msgsnd(int msgid, const void *msg_ptr, size_t msg_sz, int msgflg);
struct my_message{
long int message_type;
/* The data you wish to transfer*/
};
int msgrcv(int msgid, void *msg_ptr, size_t msg_st, long int msgtype, int msgflg);
int msgctl(int msgid, int command, struct msgid_ds *buf);
struct msgid_ds
{
uid_t shm_perm.uid;
uid_t shm_perm.gid;
mode_t shm_perm.mode;
};
公共部分,端口封装:
comm.c
/*************************************************************************
> File Name: comm.c
> Author: ZX
> Mail: [email protected]
> Created Time: Sun 14 May 2017 07:20:00 AM PDT
************************************************************************/
#include
#include "comm.h"
int commMsgQueue(int flag)
{
key_t _key = ftok(PROCPATH, PROCID);
if(_key < 0)
{
perror("ftok");
return -1;
}
int msgid = msgget(_key, flag);
if(msgid < 0)
{
perror("msgget");
return -2;
}
return msgid;
}
int creatMsgQueue()
{
return commMsgQueue(IPC_CREAT | IPC_EXCL | 0666);
}
int getMsgQueue()
{
return commMsgQueue(IPC_CREAT);
}
int destoryQueue(int msgid)
{
if(msgctl(msgid, IPC_RMID, NULL) < 0)
{
perror("msgctl");
return -1;
}
return 0;
}
int sendMsg(int msgid, int type,const char* msg)
{
struct msgbuf buf;
buf.mtype = type;
strcpy(buf.mtext, msg);
int s = msgsnd(msgid, &buf, sizeof(buf.mtext), 0);
if(s < 0)
{
perror("msgsnd");
return -1;
}
return 0;
}
int recvMsg(int msgid, int type, char out[])
{
struct msgbuf buf;
printf("begin recvMsg!\n");
if(msgrcv(msgid, (void*)&buf, sizeof(buf.mtext), type, 0) < 0)
{
perror("msgrcv");
return -1;
}
strcpy(out, buf.mtext);
printf("out: %s\n", buf.mtext);
return 0;
}
server端:
/*************************************************************************
> File Name: server.c
> Author: ZX
> Mail: [email protected]
> Created Time: Sun 14 May 2017 07:18:59 AM PDT
************************************************************************/
#include
#include "comm.h"
int main()
{
int msgid = creatMsgQueue();
printf("createMsgQueue:%d\n", msgid);
if(msgid < 0)
{
return 1;
}
char buf[1024];
while(1)
{
buf[0] = 0;
recvMsg(msgid, SERVER_TYPE, buf);
printf("recv from client#:%s\n", buf);
printf("Please Enter:\n");
fflush(stdout);
int s = read(0, buf, sizeof(buf)-1);
if(s > 0)
{
buf[s-1] = 0;
printf("read from stdout buf:%s\n", buf);
if(sendMsg(msgid, CLIENT_TYPE, buf) < 0)
{
return -1;
}
printf("send done,wait recv...\n");
}
}
//sleep(6);
destoryQueue(msgid);
return 0;
}
client端:
/*************************************************************************
> File Name: client.c
> Author: ZX
> Mail: [email protected]
> Created Time: Sun 14 May 2017 07:19:07 AM PDT
************************************************************************/
#include
#include "comm.h"
int main()
{
int msgid = getMsgQueue();
//printf("getMsgQueu:%d\n", msgid);
if(msgid < 0)
{
return 1;
}
char buf[1024];
while(1)
{
buf[0] = 0;
printf("Please Enter#:\n");
fflush(stdout);
ssize_t s = read(0, buf, sizeof(buf));
if(s > 0)
{
buf[s-1] = '\0';
sendMsg(msgid, SERVER_TYPE, buf);
printf("send done,wait recv...\n");
}
recvMsg(msgid, SERVER_TYPE, buf);
printf("recv from server# %s\n", buf);
}
return 0;
}