void send_message(int team, int target) { int msgsnd_result; t_msg qbuf; qbuf.type = (long)team; qbuf.team = team; qbuf.target = target; msgsnd_result = msgsnd(get_msg_queue(), (struct msgbuf*)&qbuf, sizeof(t_msg), 0); if (msgsnd_result == -1) printf("Error can't send message to msg queue %d\n", get_msg_queue()); }
int read_message(t_msg *qbuf, int team) { ssize_t msgrcv_ret; qbuf->type = team; msgrcv_ret = msgrcv(get_msg_queue(), (struct msgbuf*)qbuf, sizeof(t_msg), team, IPC_NOWAIT); return ((int)msgrcv_ret); }
int main() { int msg_id = get_msg_queue(); char buf[_SIZE_]; while(1) { printf("请输入:\n"); fflush(stdout); read(0,buf,sizeof(buf)-1); send_msg(msg_id,_CLIENT_TYPE_,buf); memset(buf,'\0',sizeof(buf)); recv_msg(msg_id,_SERVER_TYPE_,buf); printf("sever -> client:%s\n",buf); } return 0; }
void remove_queue(void) { msgctl(get_msg_queue(), IPC_RMID, 0); }