常用函数
int msgget (key_t key, int msgflg);
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);
int msgctl (int msqid, int cmd, struct msqid_ds* buf);
例子
为了展示例程,部分代码删减,且多个文件合并了,开发过程中,请不要模仿,这样不规范。
server
#include "server.h"
#include "server_function.h"
#include "fileoper.h"
#include "slinklist.h"
#include <stdbool.h>
#include <signal.h>
#include <assert.h>
#include "client.h"
#define PATH "/home/zhizhen/项目/本地银行"
#define SERVER 100
#define CLIENT 101int id1 ,id2;
Slink list = NULL;
Slink node = NULL;
void myexit(int sig){file_write(list);slink_destory(list);des_msg(id1);des_msg(id2);printf("退出成功\n");exit(0);
}
int server_run(){list = slink_create();assert(list != NULL);file_read(list);int id1 = create_msg(PATH,SERVER);int id2 = create_msg(PATH,CLIENT);assert(id1 != -1 && id2 != -1);while(1){ signal(SIGINT,myexit);Msg msg = {};Back back = {};int ret = 0;size_t msgsz = 0;ssize_t sz = recv_msg(id1,(void *)&msg,sizeof(msg.msg));assert(sz != -1);if(sz == 0){printf("该用户退出了\n"); node = NULL;}int opt = msg.type;switch(opt){case R:msgsz = recv_Reg(&msg,&back,list);break;case E:node = recv_Ent(&msg,&back,list);msgsz = sizeof(B_Ent);break;case G:msgsz = recv_GetM(&msg,&back,list,node);break;case S:msgsz = recv_SaveM(&msg,&back,list,node);break;case T:msgsz = recv_TranM(&msg,&back,list,node);break;case C:msgsz = recv_ChgP(&msg,&back,list,node);break;case D:msgsz = recv_Des(&msg,&back,list,node);break;}Slink next1 = list->next;int i = 1;while(next1 != NULL){Client *p = (Client *)(next1->elem);printf("-------------------\n");printf("*******%d*******\n",i);i++;printf("id:%s\n",p->id);printf("name:%s\n",p->name);printf("password:%s\n",p->password);printf("tel:%s\n",p->tel);printf("money:%d\n",p->money);printf("------------------\n");next1 = next1->next;}ret = send_msg(id2,(const void *)&back,msgsz);assert(ret != -1);}
}
client
#include "client.h"
#include <stdbool.h>
#include <assert.h>static void menu(){printf("--------%d.注册\n",R);printf("--------%d.登录\n",E);printf("--------非%d和%d即:退出\n",R,E);printf(">>>>\n");
}static void menu_Ent(){printf("******%d.取钱\n",G);printf("******%d.充值\n",S);printf("******%d.转账\n",T);printf("******%d.修改密码\n",C);printf("******%d.销户\n",D);printf("*******0.退出\n");printf(">>>>>\n");
}int create_msg(char *s,int id){key_t key = ftok(s,id);assert(key != -1);return msgget(key,IPC_CREAT|0644);
}
int send_msg(int msqid,const void *msg,size_t msgsz){return msgsnd(msqid,msg,msgsz,0);
}
int recv_msg(int msqid,void *msg,size_t msgsz){return msgrcv(msqid,msg,msgsz,0,0);
}
int des_msg(int msqid){return msgctl(msqid,IPC_RMID,NULL);
}
int client_run(){int id1 = create_msg(PATH,SERVER);int id2 = create_msg(PATH,CLIENT);assert(id1 != -1 && id2 != -1);
Beg:while(1){Msg msg = {};size_t msgsz = 0;Back back = {};int ret = 0;menu();int opt = 0;scanf("%d",&opt);switch(opt){case R:msgsz = send_Reg(&msg);break;case E:msgsz = send_Ent(&msg);break;default:printf("退出成功\n");exit(0);break;}ret = send_msg(id1,(const void *)&msg,msgsz);assert(ret != -1);ret = recv_msg(id2,(void *)&back,sizeof(back.msg));assert(ret != -1);opt = back.type;switch(opt){case R:back_Reg(&back);break;case E:{if(back_Ent(&back) == 1){while(1){menu_Ent();int opt = 0;Msg msg = {};Back back = {};size_t msgsz = 0;scanf("%d",&opt);switch(opt){case G:msgsz = send_GetM(&msg);break;case S:msgsz = send_SaveM(&msg);break;case T:msgsz = send_TranM(&msg);break;case C:msgsz = send_ChgP(&msg);break;case D:msgsz = send_Des(&msg);break;default:goto Beg;printf("退出成功!\n");break;}ret = send_msg(id1,(const void *)&msg,msgsz);assert(ret != -1);ret = recv_msg(id2,&back,sizeof(back.msg));assert(ret != -1);opt = back.type;switch(opt){case G:back_GetM(&back);break;case S:back_SaveM(&back);break;case T:back_TranM(&back);break;case C:back_ChgP(&back);break;case D:back_Des(&back);break;}}}else{printf("登录失败!\n");}break;}}}
}