Thread: Unable to join MQ

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    2

    Unable to join MQ

    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:
    ...
    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?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You are treating "buf.mtext" as a "key" - but server is sending the q-id, not the key.

    gg

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    2
    Quote Originally Posted by Codeplug View Post
    You are treating "buf.mtext" as a "key" - but server is sending the q-id, not the key.

    gg
    gg indeed, totally missed that. No one on stackoverflow got it either

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to join this two together?
    By SuperMiguel in forum C Programming
    Replies: 7
    Last Post: 06-20-2012, 02:01 AM
  2. Unable to join two strings due to Segmentation Fault
    By Orange Lozenge in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2011, 04:00 PM
  3. fork/join
    By modulo_crt in forum C Programming
    Replies: 10
    Last Post: 06-08-2009, 05:29 PM
  4. Join the group
    By vasanth in forum C Programming
    Replies: 1
    Last Post: 01-16-2002, 11:07 AM

Tags for this Thread