Search code examples
cibm-mqmqoracle-pro-c

mqrc 2018 error while call is made to remote mq using MQOPEN method PRO*C


I am trying to connect Remote MQ using pro*c language , While connecting to MQ i am using MQCONNX and MQOPEN methods,
After execution of MQOPEN method it returns an error MQRC 2018.
Below is the code for reference .
Also want to know is there any kind of authentication problem or server level problem causing this , As the same code with different method is working perfectly while connecting to local MQ client.

Thanks

int MS_MQ_Open(const char *chr_p_qmname, const char *chr_p_qname,
               const char *mode, MQHCONN *Hcon, MQHOBJ *Hobj,
               INTL_ENV_DATA_STRUCT_H *p_intlenv_data_struct_h,
               DEBUG_INFO_STRUCT_H **l_debug_info_ptr) {
  MQOD od = {MQOD_DEFAULT}; /* Object Descriptor             */
  MQLONG OpenCode;          /* MQOPEN completion code        */
  MQLONG O_options;
  MQLONG Reason;
  MQLONG CReason;
  MQLONG CompCode;
  MQCNO mqcno = {MQCNO_DEFAULT};          /* Connection options */
  MQCD mqcd = {MQCD_CLIENT_CONN_DEFAULT}; /* Channel Defs */
  MQCSP csp = {MQCSP_DEFAULT};
  MQCHAR chr_l_qmname[MQ_Q_MGR_NAME_LENGTH];
  char userId[50];
  char password[50];
  strncpy(userId, "XXXXXX", 50);
  strncpy(chr_l_qmname, chr_p_qmname, MQ_Q_MGR_NAME_LENGTH);
  strncpy(mqcd.ConnectionName, "10.000.00.00(port number)",
          MQ_CONN_NAME_LENGTH);
  strncpy(mqcd.ChannelName, "SVRCONN", MQ_CHANNEL_NAME_LENGTH);
  mqcno.SecurityParmsPtr = &csp;
  mqcno.Version = MQCNO_VERSION_5;
  csp.AuthenticationType = MQCSP_AUTH_USER_ID_AND_PWD;
  csp.CSPUserIdPtr = userId;
  csp.CSPUserIdOffset = 0;
  csp.CSPUserIdLength = strlen(userId);
  strncpy(password, "XXXX", 50);
  csp.CSPPasswordPtr = password;
  csp.CSPPasswordOffset = 0;
  csp.CSPPasswordLength = strlen(password);
  mqcno.ClientConnPtr = &mqcd;
  mqcno.Version = MQCNO_VERSION_5;

  MQCONNX(chr_l_qmname, &mqcno, &Hcon, &CompCode, &CReason);
  if (CompCode == MQCC_FAILED) {
    printf("MQCONNX ended with reason code |%ld|\n", CReason);
  }
  strncpy(od.ObjectName, chr_p_qname, (size_t)MQ_Q_NAME_LENGTH);
  if (!strcmp(mode, "I")) {
    O_options = MQOO_INQUIRE + MQOO_FAIL_IF_QUIESCING;
  } else if (!strcmp(mode, "O")) {
    O_options = MQOO_OUTPUT /* open queue for output        */
                + MQOO_FAIL_IF_QUIESCING +
                MQOO_SET_ALL_CONTEXT; /* but not if MQM stopping      */
  } else {
    printf("Invalid mode %s\n", mode);
    APL_GOBACK_FAIL
  }
  MQOPEN(Hcon, &od, O_options, &Hobj, &OpenCode, &Reason);
  if (Reason != MQRC_NONE) {
    printf("MQOPEN ended with reason code |%ld|\n", Reason);
  }
}

Solution

  • The MQ Reason code 2018 (MQRC_HCONN_ERROR) means that your Hcon parameter is not correct. Your code tests for the CompCode being MQCC_FAILED, but does not check for MQCC_WARNING. I suggest that you change this test to the following:-

    if (CompCode != MQCC_OK) {
      printf("MQCONNX ended with reason code |%ld|\n", CReason);
    }
    

    It is possible that you have run into one of the few MQCC_WARNING reason codes and that the Hcon is not set for the one you have hit.

    Good practice is to set your Hcon parameter to the value MQHC_UNUSABLE_HCONN prior to making a call to MQCONN or MQCONNX, and then test that the Hcon parameter is no longer set to that value before making a call to something like MQOPEN. This will mean that you can be certain that your MQCONN(X) call set the Hcon parameter to something that should not cause a 2018.

    if (Hcon != MQHC_UNUSABLE_HCONN)
    {
      MQOPEN(Hcon, &od, O_options, &Hobj, &OpenCode, &Reason);
      if (Reason != MQRC_NONE) {
        printf("MQOPEN ended with reason code |%ld|\n", Reason);
      }
    }