简体   繁体   中英

My client doesn't send correctly to server

I have problems with my code.

I have this client with posix queue. Is a posix queue comunication with a conc server. The question is down.

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");

And this server

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);

AND HERE IS THE PROBLEM. WHEN I PRINF MESS.OP I WOULD RECIVE "3". The number that I asigned the client. And when I do this, mess.op have 0, not 3.

pthread_create(&thid, &t_attr, (void*)func, &mess);
...
...
...
...

}

Thanks.

It would appear that you are calling pthread_create() with a local ( or automatic ) variable mess which is sometimes deallocated before the thread is actually started. You need to allocate mess on the heap and be very careful to free it at the appropriate time.

Also (void *) func doesn't look right because,

and it would appear as if you were ignoring compilation warnings, or forcing them to disappear by randomly casting a pointer to the type the compiler suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM