#include "wrapper.h" char *mygetpid(char *str); char *getfirst(char *str); typedef struct MESSAGE { long mtype; char mtext[512]; } mymsg, *pmymsg; int main(int argc, char *argv[]) { int rtn; int msqid; key_t key; mymsg msginfo; char first[512]; char buf[512]; if (argc != 2) { fprintf(stderr, "请以./task72s<key>的形式运行给出的消息队列键值\n"); exit(0); } sscanf(argv[1], "%x", &key); msqid = msgget(key, 0644); msginfo.mtype = 1; while (1) { memset(msginfo.mtext, 0, sizeof(msginfo.mtext)); rtn = msgrcv(msqid, (pmymsg)&msginfo, sizeof(msginfo.mtext), 1, 0); if (rtn == -1) continue; strcpy(first, msginfo.mtext); printf("收到消息:%s\n", first); strcat(buf, "receipt of "); strcat(buf, getfirst(msginfo.mtext)); strcat(buf, " from 白校珲's server to Process "); strcat(buf, mygetpid(first)); strcpy(msginfo.mtext, buf); rtn = msgsnd(msqid, &msginfo, strlen(msginfo.mtext), 0); sleep(1); memset(buf, 0, sizeof(buf)); if (strcmp(first, "close") == 0) { return 0; } } } //服务端提取消息对应的线程 char *mygetpid(char *str) { char *p[21]; memcpy(p, str, 4); char *t; p[0] = strtok(str, " "); int i = 0; while (p[i] != NULL) { i++; p[i] = strtok(NULL, " "); } t = p[i - 1]; return t; } char *getfirst(char *str) { char *p[21]; memcpy(p, str, 4); char *t; p[0] = strtok(str, " "); t = p[0]; return t; }
最新发布