假设有一个服务器,有多个客户端访问它,怎么借助消息队列,让它把a客户端的回复消息只发给a,而b客户端的回复消息只发给b。
实现:
我们让所有客户端都用1号通道给服务器发送消息,把它们各自的进程PID写进数据的开头,然后我们回复的时候,就用它们的PID通道发送,客户端接收也在PID通道接收。
首先是服务器程序:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include<signal.h>
struct msgbuf{
long type;
char text[100];
};
int id;
void handler(int s){
msgctl(id, IPC_RMID, 0);
exit(0);
}
int main( void ) {
struct sigact