I. Introduction
When learning UNIX Network Programming Volume 1, we can use socket Socket socket to implement the echo client/server program, but socket programming has some shortcomings, such:
1. When the server must be started, the client can connect to the server and communicate with the server;
2. The peer IP address and port must be known for communication by using the set interface descriptor.
II. Introduction to related functions
Next, we use the System V message queue to implement inter-process communication:
First, let's take a look at the following functions:
1. msgget: This function is used to open or create a message queue. Its function is equivalent to the file operation function open.
#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>int msgget(key_t key, int msgflg);
2. msgsnd: Message sending Function
3. msgrcv: message receiving function
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
4. struct msgbuf
struct msgbuf { long mtype; /* message type, must be > 0 */ char mtext[1]; /* message data */
};
Mtype Message Type
Mtext message content
Iii. Implementation Principle
Server: only receives messages of Type 1. After receiving the message, extract the first four bytes of mtext and store them as client_pid. Then, modify the mtype of the Message Type to client_pid;
Client: only send messages of type 1, that is, set mtype to 1, and set the content of the first four bytes of mtext to its own process ID, that is, PID.
4. Programming implementation:
Client:
/************************************************************************* > File Name: echocli.c > Author: ma6174 > Mail: [email protected] > Created Time: Tue 28 Oct 2014 11:25:48 AM HKT ************************************************************************/#include<stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdlib.h>#include <errno.h>#include <string.h>#define ERR_EXIT(m) do { perror(m); exit(EXIT_FAILURE); } while(0)#define MSGMAX 8192struct msgbuf{ long mtype; /* type of message */ char mtext[MSGMAX]; /* message text */};void echo_cli(int msgid){ int pid = getpid(); int n; struct msgbuf msg; memset(&msg, 0, sizeof(msg)); msg.mtype = 1; *((int*)msg.mtext) = pid; while(fgets(msg.mtext + 4, MSGMAX, stdin) != NULL) { msg.mtype = 1; if(msgsnd(msgid, &msg, 4 + strlen(msg.mtext + 4), IPC_NOWAIT) < 0) ERR_EXIT("msgsnd"); memset(msg.mtext + 4, 0, MSGMAX - 4); if((n = msgrcv(msgid, &msg, MSGMAX, pid, 0)) < 0) ERR_EXIT("msgrcv"); fputs(msg.mtext + 4, stdout); memset(msg.mtext + 4, 0, MSGMAX - 4); }}int main(int argc, char **argv){ int msgid; msgid = msgget(1234, 0);//open if(msgid == -1) ERR_EXIT("msgget"); echo_cli(msgid); return 0;}
Server:
/************************************************************************* > File Name: echosrv.c > Author: ma6174 > Mail: [email protected] > Created Time: Tue 28 Oct 2014 11:25:58 AM HKT ************************************************************************/#include<stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdlib.h>#include <errno.h>#include <string.h>#define ERR_EXIT(m) do { perror(m); exit(EXIT_FAILURE); } while(0)#define MSGMAX 8192struct msgbuf{ long mtype; /* type of message */ char mtext[MSGMAX]; /* message text */};void echo_srv(int msgid){ int n; struct msgbuf msg; memset(&msg, 0, sizeof(msg)); while(1) { //only recv the message of type = 1 if((n = msgrcv(msgid, &msg, MSGMAX, 1, 0)) < 0) { ERR_EXIT("msgrcv"); } fputs(msg.mtext + 4, stdout); //client pid int pid; pid = *((int*)msg.mtext); //回射 msg.mtype = pid;//type msgsnd(msgid, &msg, n, 0); memset(&msg, 0, sizeof(msg)); }}int main(int argc, char **argv){ int msgid; msgid = msgget(1234, IPC_CREAT | 0666); if(msgid == -1) { ERR_EXIT("msgget"); } echo_srv(msgid); return 0;}
Problems:
1. Different processes cannot communicate with different hosts.
2. Message length restricted msgmax
3. Maximum number of messages received msgmnb
4. Deadlocks may occur.
Use the System V message queue to achieve customer/Server bounce