我正在尝试使用 pro*c 语言连接远程 MQ,在连接到 MQ 时我使用的是 MQCONNX 和 MQOPEN 方法,执行
MQOPEN 方法后返回错误 MQRC 2018。
以下是供参考的代码。
还想知道是否有任何类型的身份验证问题或服务器级别问题导致这种情况,因为相同的代码在连接到本地MQ客户端时具有不同的方法可以正常工作。
谢谢
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 %sn", 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);
}
}
MQ 原因代码 2018 (MQRC_HCONN_ERROR( 表示您的Hcon
参数不正确。您的代码测试CompCode
是否MQCC_FAILED
,但不检查MQCC_WARNING
。我建议您将此测试更改为以下内容:-
if (CompCode != MQCC_OK) {
printf("MQCONNX ended with reason code |%ld|n", CReason);
}
您可能遇到了为数不多的MQCC_WARNING
原因代码之一,并且未为您命中的Hcon
设置。
好的做法是在调用 MQCONN 或 MQCONNX 之前将Hcon
参数设置为值MQHC_UNUSABLE_HCONN,然后在调用 MQOPEN 之类的东西之前测试Hcon
参数是否不再设置为该值。这意味着您可以确定您的 MQCONN(X( 调用将Hcon
参数设置为不会导致 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);
}
}