我的代碼有問題。我的客戶端沒有正確發送到服務器
我有這個客戶與posix隊列。是一個posix隊列與濃縮服務器通信。 問題是關閉的。
mqd_t server;
mqd_t client;
struct request req; //char *name, int pos, int op...
struct reply rep; //int num, int ret...
struct mq_attr attr;
attr.mq_maxmsg = 1;
attr.mq_msgsize = sizeof(struct reply);
client = mq_open("/CLIENT", O_CREAT|O_RDONLY, 0700, &attr);
server = mq_open("/SERVER", O_WRONLY);
req.name = name;
req.op = 3;
mq_send (server, (const char *) &req, sizeof(struct req),0);
mq_receive(client, (char*)&rep, sizeof(struct reply), 0);
mq_close(server);
mq_close(client);
mq_unlink("/CLIENT");
而這個服務器
struct db mydb[100]; //Struct with char*name....
for (int i = 0; i<100;i++){
strcpy(mydb[i].name,"");
mydb[i].vec =NULL;
mydb[i].size =0;
}
mqd_t server;
struct request mess;
struct mq_attr q_attr;
pthread_attr_t t_attr;
q_attr.mq_maxmsg = 10;
q_attr.mq_msgsize=sizeof(struct request);
server = mq_open ("/SERVER", O_CREAT|O_RDONLY, 0700, &q_attr);
if (server == -1){
perror("Er");
}
pthread_mutex_init (&mutex_msj, NULL);
pthread_cond_init (&cond_msj, NULL);
pthread_attr_init(&t_attr);
pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED);
while(1){
pthread_t thid;
mq_receive(server, (char*) &mess, sizeof(struct request), 0);
這裏就是問題所在。當我PRINF MESS.OP我會記錄「3」。我簽下客戶的號碼。當我這樣做,mess.op有0,而不是3.
pthread_create(&thid, &t_attr, (void*)func, &mess);
...
...
...
...
}
謝謝。
你的代碼太亂了,你能格式化它,所以它的可讀性? –
請閱讀旅程,不要這樣做。 –