简体   繁体   中英

C program with MQ get a message MQGET ended with reason code 2005

I made and run the C program, which connect to MQ and try to get a message. I always get a message:

         MQGET ended with reason code 2005        

this means:

           MQRC_BUFFER_LENGTH_ERROR (2005, X'7D5') Buffer length parameter not valid    

I declared the buffer as

           char     TempBuf[65536];    

The message in MQ is

            "This is a test message"    

in MQ error log nothing is written.

Below is the program:

               **/* header files */**    
               #include <stdio.h>   
               #include <stdlib.h>      
               #include <string.h>      

               #include <cmqc.h>      /* includes for MQI*/              
               #include <cmqxc.h>   

               int main(int argc, char **argv)    
               {
               MQCNO   Connect_options = {MQCNO_DEFAULT};/MQNONNX opt*/   
               MQCD    ClientConn = {MQCD_CLIENT_CONN_DEFAULT};/*client channel*/  
               MQHCONN  Hcon;                   /* connection handle  */  
               MQHOBJ   Hobj;                   /* object handle */   
               MQLONG   CompCode;               /* completion code  */   
               MQLONG   OpenCode;               /* MQOPEN completion code*/  
               MQLONG   Reason;                 /* reason code    */   
               MQOD     od = {MQOD_DEFAULT};    /* Object Descriptor */   
               MQMD     md = {MQMD_DEFAULT};    /* Message Descriptor */   
               MQPMO    pmo = {MQPMO_DEFAULT};  /* put message options*/   
               MQLONG   O_options;              /* MQOPEN options  */   
               MQLONG   C_options;              /* MQCLOSE options */   
               MQGMO   gmo = {MQGMO_DEFAULT};   /* get message options */   

               char     QMgrName[MQ_Q_MGR_NAME_LENGTH+1];
               char     QName[MQ_Q_NAME_LENGTH+1];
               char     channelName[MQ_CHANNEL_NAME_LENGTH+1];
               char     hostname[1024];
               char     port[4];
               MQLONG   buflen;        /* buffer length*/
               char TempBuf[65536];
               int msgsToGet;
               int msgsGot;

               if (argc != 6)   
               {   
                printf("Usage: MQTest11 QMgrName ChlName hostname port  QName\n");   
                return(1);   
               }    

               **/* copy MQ manager name */**   
               strncpy(QMgrName, argv[1], MQ_Q_MGR_NAME_LENGTH);    
               QMgrName[MQ_Q_MGR_NAME_LENGTH] = '\0';    

               **/* copy channel name */**
               strncpy(channelName, argv[2], MQ_CHANNEL_NAME_LENGTH);    
               channelName[MQ_CHANNEL_NAME_LENGTH] = '\0';    

               /* copy hostname */
               strncpy(hostname, argv[3], 1023);    
               hostname[1023] = '\0'; 

               /* copy port number */   
               strncpy(port,argv[4],4);    
               strncpy(QName, argv[5], MQ_Q_NAME_LENGTH);    
               QName[MQ_Q_NAME_LENGTH] = '\0';    

               /* copy hostname for connection */
               strncpy(ClientConn.ConnectionName,hostname, MQ_CONN_NAME_LENGTH);    

               /* copy channel name */
               strncpy(ClientConn.ChannelName,channelName,MQ_CHANNEL_NAME_LENGTH);   

               /* Point the MQCNO to the client connection definition */   
               Connect_options.ClientConnPtr = &ClientConn;    
               Connect_options.Version = MQCNO_VERSION_2;    

               /* use MQCONNX */    

               if (CompCode == MQCC_FAILED)    
               {   
                    /* exit with print the reason */    
               }    
               else    
               {    
                strncpy(od.ObjectName, QName, (size_t)MQ_Q_NAME_LENGTH);    
                O_options = MQOO_OUTPUT + MQOO_FAIL_IF_QUIESCING;    
                /* use MQOPEN */    

                if (OpenCode == MQCC_OK)    /* if MQOPEN , then continue in the while loop */      
                {    
                gmo.Options = MQGMO_WAIT + MQGMO_CONVERT;    
                gmo.WaitInterval = 15000;    
                msgsGot = 0;    
                msgsToGet = 0;    

                }    
                 while (CompCode != MQCC_FAILED && ((msgsToGet == 0) || (msgsGot < msgsToGet)))    
                {   

               /* define length of the buffer -1 */   
               buflen = strlen(TempBuf) - 1;    */ buffer length */
               memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId)); /*copy msg ID*/     
               memcpy(md.CorrelId, MQCI_NONE, sizeof(md.CorrelId));/*copy corrlID*/      
               md.Encoding       = MQENC_NATIVE; /*encode*/   
               md.CodedCharSetId = MQCCSI_Q_MGR;   

               /* function to get message from MQ*/
               MQGET(Hcon,       /* get message from MQ */   
               Hobj,             /* object handle*/   
               &md,              /* message descriptor*/   
               &gmo,             /*get message options*/    
               buflen,           /*buffer length*/   
               TempBuf,          /* buffer */   
               &messlen,         /* message length*/   
               &CompCode,       /* completion code*/   
               &Reason);         /* reason code*/   

             **/* I put some statements to check if transaction failed or not*/**    
               if (Reason != MQRC_NONE)     
               {   
               if (Reason == MQRC_NO_MSG_AVAILABLE)   
               {   
               /* print statement no more messages */   
               else   
               {   
                 printf("MQGET ended with reason code %d comcode %d\n",Reason,CompCode);       
                if (Reason == MQRC_TRUNCATED_MSG_FAILED)    
                {   
                 /print statement that it is failed*/    
                }     
                }   
                }   

               **/* This is almost done, only statement if Compcode not failed, then print buffer */**

The buffer is not printed and error message you can see above. I appreciate any help. Thanks

The buffer should be of type MQBYTE not char :

Change: char TempBuf[65536];
To: MQBYTE TempBuf[65536];

To get the buffer length you should use sizeof not strlen .

Change: buflen = strlen(TempBuf) - 1;
To: buflen = sizeof(TempBuf)-1;

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