I am using SysV for sending/receiving messages.
Inside the thread function of my Server class is where I create the key and MQ. I have tested that this is done successfully and the mqID of the new MQ is stored in the variable mtext in a struct msgbuf.
Code:
struct msgbuf {
long mtype;
char mtext[100];
};
This is how I create the mqID in the thread function of the server class:
Code:
void*thread_function(void*arg){
int temp;//temp placement for current client
temp=i;
key_t keyT;
struct msgbuf bufT;
keyT=ftok("mcs1.c", 'B'); //create key for client[temp]
if ((client[temp].mqID=msgget(keyT, 0666 | IPC_CREAT))==-1){ perror("msgget"); }
bufT.mtype=2; //1. SET MTYPE TO 2
sprintf(bufT.mtext, "%d\n", client[temp].mqID);//2. SET MTEXT TO MQID OF PRIVATE MQ
if (msgsnd(msqid, &bufT, sizeof(bufT.mtext),0)==-1) {perror("msgsnd");} //3. SEND TO CLIENT using global MQ
...
}
So the MQID of the new MQ i just created is stored in bufT, which is then sent as a message to a client class through the global MQ created in the Servers main ( I will post the code if it will help)
This is how the Client class receives the message from the server, takes the MQID and attempts to join the MQ:
Code:
if(msgrcv(msqid, &buf, sizeof(buf.mtext), 2, 0) == -1){ //RECEIVE ACK MSG
perror("msgrcv");
}
else{
printf("ack received\n");
}
key_t key1;
key1=atoi(buf.mtext);
if ((mqT=msgget(key1, 0666)) == -1){
printf("UNABLE TO JOIN PRIVATE MQ");
perror("msgget");
printf("%d", key1);
}
...
Whenever I run the server and then a client, it says msgget: No such file or directory on the terminal running the client program. Even though when I print the key in the client class and is the correct key, it still can't join the MQ. What is the reason for this?